Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible wait for ssh is not working

Tags:

ansible

Ansible wait_for ssh stopped working. Ansible playbook is able to launch ec2 instance, the wait_for module is being used to wait for ssh to start. Ansible times out while waiting for ssh.

My playbook

- 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 }}"

Error trace

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

}) => {
    "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"

Ansible does not return anything for public_dns_name and public_ip is "null".

As a result the wait_for ssh fails, even though ec2 is spawned properly and I am able to ssh to machine via appropriate key manually.

What I am doing wrong?

like image 430
ZzzZ Avatar asked Sep 08 '25 11:09

ZzzZ


1 Answers

- 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 }}"

You are using the with_items construct, but I don't see any use of the item variable. I don't use AWS, but perhaps you need

 - name: Wait for ssh to come up
   wait_for: host="{{ item.public_dns_name }}" port=22 delay=10  timeout=300
   with_items: "{{ ec2.instances }}"
like image 140
hymie Avatar answered Sep 11 '25 03:09

hymie