I could not find the real reason why the SSH on EC2 instance running CentOS 
was not able to connect. The workaround I applied was to write a module 
which try to connect host over SSH for given number of times and return 
success when it is successful so that further plays won't fail because of 
unreachable error.

*wait_for_SSH.py*
#!/usr/bin/python
'''
module: wait_for_SSH
short_description: Waits for a host to be SSH connectable.
description:
     - Tries to Ansible ping(not ICMP ping) to host as per the passed 
parameters
options:
  host:
    description:
      - A resolvable hostname or IP address to ansible ping
    required: true
  retries:
    description:
      - maximum number of times to retry
    required: false
    default: 10
  delay:
    description:
      - number of seconds to wait between two consecutive pings
    required: false
    default: 5
'''
from ansible.module_utils.basic import *
from subprocess import call
import time

def validate_params(module, retries, delay):
  if retries < 0:
    module.fail_json(msg="retries should be greater than 0")
  if delay < 0:
    module.fail_json(msg="delay should be greater than 0")   
  return

def main():
    fields = {
      "host": {"required": True, "type": "str"},
      "retries": {"required": False, "type": "int", "default": 10},
      "delay": {"required": False, "type": "int", "default": 5}
    }
    module = AnsibleModule(argument_spec=fields)
    host = module.params['host']
    retries = module.params['retries']
    delay = module.params['delay']
    count = 0
    output = 1
    validate_params(module, retries, delay)
    while (count < retries) and (output != 0):
      if delay:
        time.sleep(delay)
      output = call(["ansible", "all", "-i", ","+host, "-m", "ping"])
      count += 1
    response = {"output" : output}
    module.exit_json(changed=False, output=response)
    
if __name__ == '__main__':
    main()


And executed it after wait_for port 22.

...  
  # Wait only for running instances because 'ec2_server' might contain 
terminated instances to fulfil exact_count condition
  - name: wait for ssh server to be running
    wait_for: host={{ item.public_dns_name }} port=22 search_regex=OpenSSH
    with_items: "{{ec2_server.instances | default([])}}"
    when: item.state == 'running'
  
  # wait_for_SSH is our custom module which tries to Ansible ping on 
created instances until it is successful as per retries specified(bug AD-3)
  - name: Ensure SSH is running
    wait_for_SSH:
      host: "{{item.private_ip}}"
    register: moduleoutput
    with_items: "{{ec2_server.instances | default([])}}"
    when: item.state == 'running'
..<<further plays>>...

Thanks,
Nirav

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/ad02fcad-284b-4418-8390-4a77e9aaab37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to