ansible / terraform-provider-ansible

community terraform provider for ansible
https://registry.terraform.io/providers/ansible/ansible/latest
GNU General Public License v3.0
183 stars 42 forks source link

Ansible Playbook | ssh timeout | time_sleep #123

Closed willifehler closed 2 weeks ago

willifehler commented 1 month ago

Hey there,

is there any best practices to execute an Ansible Playbook resource after a VM was created with another Provider? Let's say I'm creating a VM in VMware vSphere and it takes time to boot the VM. Sometimes the Ansible Playbook will fail becase ssh isn't ready yet. My current approach is to execute a time_sleep resource which is executed all the time but this isn't great.

Regards - Willi

ceejatec commented 3 weeks ago

Assuming your VM resource waits until the correct IP address is known (which the vsphere provider should - look at the wait_for_guest_* options if that's not working), you could have a task at the beginning of your playbook like:

- name: Wait for new VM to be connectable
  wait_for_connection:
    timeout: 300
    sleep: 3

That will keep trying to connect to the IP every 3 seconds until it succeeds.

gravesm commented 2 weeks ago

In addition to what @ceejatec has suggested, you can also try configuring the ssh connection plugin to retry (https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html).