title: Ansible wait for ssh is not working
body: <p>Ansible <code>wait_for</code> ssh stopped working. Ansible playbook is able to launch ec2 instance, the <code>wait_for</code> module is being used to wait for ssh to start. Ansible times out while waiting for ssh.</p>
  
  <p>My playbook</p>
  
  <pre><code>- name: Configuring and lanuching EC2 instance
    hosts: localhost
    connection: local
    vars_files:
      - general_vars
  
    tasks:
       - name: Launch instance
         ec2:  
          instance_type: "{{ instance_type }}"
          image: "{{ image_ami }}"
          region: "{{ region }}"
          vpc_subnet_id: "{{ subnet_id }}"
          assign_public_ip: yes
          group_id: "{{ security_group }}"
          key_name: "{{ key_pair }}" 
          volumes:
            - device_name: /dev/xvda
              volume_type: gp2
              volume_size: 8
  
         register: ec2
  
       - name: Wait for ssh to come up
         wait_for: host="{{ ec2.instances[0].public_dns_name }}" port=22 delay=10  timeout=300
         with_items: "{{ ec2.instances }}"
       - name: Save the Ip Address of the machine
         add_host:
             hostname: "{{ item.public_ip }}"
             groupname: ec2_instance_ips
         with_items: "{{ ec2.instances }}"
  </code></pre>
  
  <p>Error trace</p>
  
  <pre><code>The full traceback is:
    File "/var/folders/y3/t87nx4q95w9_jmg80csrn6bc0000gn/T/ansible_SpGSRH/ansible_module_wait_for.py", line 540, in main
      s = _create_connection(host, port, min(connect_timeout, alt_connect_timeout))
    File "/var/folders/y3/t87nx4q95w9_jmg80csrn6bc0000gn/T/ansible_SpGSRH/ansible_module_wait_for.py", line 405, in _create_connection
      connect_socket = socket.create_connection((host, port), connect_timeout)
    File "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 575, in create_connection
      raise err
  
  }) =&gt; {
      "changed": false,
      "elapsed": 300,
      "invocation": {
          "module_args": {
              "active_connection_states": [
                  "ESTABLISHED",
                  "FIN_WAIT1",
                  "FIN_WAIT2",
                  "SYN_RECV",
                  "SYN_SENT",
                  "TIME_WAIT"
              ],
              "connect_timeout": 5,
              "delay": 10,
              "exclude_hosts": null,
              "host": "",
              "msg": null,
              "path": null,
              "port": 22,
              "search_regex": null,
              "sleep": 1,
              "state": "started",
              "timeout": 300
          }
      },
      "item": {
          "ami_launch_index": "0",
          "architecture": "x86_64",
          "block_device_mapping": {},
          "dns_name": "",
          "ebs_optimized": false,
          "groups": {
              "sg-09664e62": "yyyzzzzbbbbbb"
          },
          "hypervisor": "xen",
          "id": "i-XXxxxxxxxxxxxxxxx",
          "image_id": "XXXXXXXXXXXXX",
          "instance_type": "t2.micro",
          "kernel": null,
          "key_name": "XXXX",
          "launch_time": "XXXXXX",
          "placement": "ccccccccccccccc",
          "private_dns_name": "",
          "private_ip": "XXXXX",
          "public_dns_name": "",
          "public_ip": null,
          "ramdisk": null,
          "region": "XXXX",
          "root_device_name": "/dev/sda1",
          "root_device_type": "ebs",
          "state": "pending",
          "state_code": 0,
          "tags": {},
          "tenancy": "default",
          "virtualization_type": "hvm"
      },
      "msg": "Timeout when waiting for :22"
  </code></pre>
  
  <p>Ansible does not return anything for <code>public_dns_name</code> and <code>public_ip</code> is "null".</p>
  
  <p>As a result the <code>wait_for</code> ssh fails, even though ec2 is spawned properly and I am able to ssh to machine via appropriate key manually.</p>
  
  <p>What I am doing wrong?</p>