1

How do you get ansible to wait or retry ssh connections? I have an ansible tsk that runs govc to upload a vm into vcenter but right after that I ssh into the machine to run commands like this:

  hosts: vcenter
  gather_facts: false
  tasks:
    - name: Download GOVC
      get_url:
        url: https://github.com/vmware/govmomi/releases/download/v0.20.0/govc_linux_amd64.gz
        dest: /home/gkeadmin/govc_linux_amd64.gz

but doing it right after I get this: fatal: [139.178.66.91]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 1.2.3.4 port 22: Operation timed out", "unreachable": true}

I rerun it again with the --retry command and then it continues. Seems like it just needs sometime before I can connect via ssh...how do I wait for an ssh connection to get established in ansible?

lightweight
  • 3,227
  • 14
  • 79
  • 142
  • Possible duplicate of [How to retry Ansible task that may fail?](https://stackoverflow.com/questions/44134642/how-to-retry-ansible-task-that-may-fail) – ceving Mar 22 '19 at 08:56

2 Answers2

0

ansible supports retries. May this can help you.

---
- name: test
  retries: 2
  hosts: <hosts_name>
  tasks:
    - name: task
      <module_name>:
error404
  • 2,684
  • 2
  • 13
  • 21
0

you can add a section on the top of your playbook to wait for it, for example

---
- name: wait for ssh
  tasks:
  - wait_for:
      port: 22
      host: '{{ inventory_hostname }}'
    delegate_to: localhost
- name: my playbook
  hosts: vcenter
  gather_facts: false
  tasks:
   - name: Download GOVC
  [ ... etc ... ]

https://docs.ansible.com/ansible/latest/modules/wait_for_module.html#examples

user2599522
  • 3,005
  • 2
  • 23
  • 40