2
votes

I am trying to ssh to an ec2 instance, via my ansible-playbook file. I can ssh into the instance locally, and currently have the following configuration in my ansible-playbook file:

- name: Wait for SSH to come up
  delegate_to: "{{ public_dns }}"
  wait_for_connection:
    delay: 60
    timeout: 120
  with_items: "{{ public_ip }}"
  remote_user: ubuntu
  connection: ssh
  register: item

However, when I run this, I get the following error:

failed: [localhost -> {{ public_dns }}] (item={'_ansible_item_result': True, '_ansible_no_log': False, '_ansible_delegated_vars': {'ansible_delegated_host': u'{{ public_dns }}', 'ansible_host': u'{{ public_dns }}'}, 'changed': False, 'elapsed': 184, 'failed': True, 'item': u'{{ public_ip }}', 'msg': u"timed out waiting for ping module test success: 'ping'", '_ansible_ignore_errors': None}) => {"changed": false, "elapsed": 184, "item": "{{ public_ip }}", "msg": "timed out waiting for ping module test success: 'ping'"}

When I ping the public_dns or public_ip value from my machine both are successful.

Why would the ping event fail when running the Ansible file? (I'm not purposefully trying to ping the instance, I actually want to connect to it via ssh. However the error shows that Ansible may be trying to ping the instance before connecting, based on the "timed out waiting for ping module test success")

I have since changed the method to:

- name: Add new instance to host group
  add_host:
  hostname: ""{{ public_ip }}"
  groupname: launched

- name: Wait for SSH to come up
  wait_for:
    host: launched
    port: 22
    sleep: 30
    delay: 10
    timeout: 360
  state: started

However, this has been throwing a different error, of "Timeout when waiting for launched:22" The traceback includes the following:

connect_socket = socket.create_connection((host, port), connect_timeout) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 557, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM):.

Any ideas?

1
there is two " in hostname: ""{{ public_ip }}" but I think it's just a wrong copy paste in stackoverflowbast

1 Answers

2
votes

Please note that Ansible ping module is not the same as ICMP Ping: https://docs.ansible.com/ansible/2.5/modules/ping_module.html. Ansible will try to connect to the target host via ssh when using the ping module

If the above task is inside a playbook that is run against a newly provisioned ec2 instance, I would set gather_facts: false and run the wait_for_connection as my first task. If it still fails, try increasing the timeout parameter