# Retry SSH connection?

**URL:** <https://forum.ansible.com/t/retry-ssh-connection/14125>\
**Category:** Ansible Project\
**Created:** [December 11, 2013, 2:31pm UTC](https://forum.ansible.com/t/retry-ssh-connection/14125 "2013-12-11T14:31:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Hunter](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron\_Hunter](https://forum.ansible.com/u/Aaron_Hunter)\
**Post date:** [December 11, 2013, 2:31pm UTC](https://forum.ansible.com/t/retry-ssh-connection/14125/1 "2013-12-11T14:31:01Z")

</div>

I have a complex playbook that will provision a CentOS VM on a XenServer pool from scratch and then configure the VM according to its roles. It works very well (and I’m very impressed with Ansible) except for one part. As XenServer installs the VM and OS (via a delegate\_to call in the Task) it takes time and the VM is not yet online. The next step in my Ansible script tries to connect to the new and still offline VM and promptly fails. Neither ‘async’ nor ‘do-until’ have worked. It always fails because Ansible cannot connect via SSH to the newly spun up VM.

My question is, how can I get this task to keep trying to connect? I could not find anything in the docs or Google to suggest an approach.

Thank you,  
Aaron

DevOps Blog: [http://www.sharknet.us](http://www.sharknet.us)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 11, 2013, 3:05pm UTC](https://forum.ansible.com/t/retry-ssh-connection/14125/2 "2013-12-11T15:05:02Z")

</div>

This is a great place to use the “wait\_for” module to wait for the port to become open.

---

<div class="post-metadata">

**Author:** ![Aaron\_Hunter](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Aaron\_Hunter](https://forum.ansible.com/u/Aaron_Hunter)\
**Post date:** [December 11, 2013, 4:20pm UTC](https://forum.ansible.com/t/retry-ssh-connection/14125/3 "2013-12-11T16:20:16Z")

</div>

Michael,

Thank you. Looks like you already anticipated the need for this. Sorry I missed it.

It worked. For the sake of others, here is the statement that accomplished the task. It wouldn’t work without the “local\_action” part. I put this in my “main.yml” under the “vm” role.

[VM creation tasks…]

- name: Wait for the Kickstart install to complete and the VM to reboot  
local\_action: wait\_for host={{ vm\_hostname }} port=22 delay=30 timeout=1200 state=started

- name: Now configure the VM…

–Aaron  
DevOps Blog: [http://www.sharknet.us](http://www.sharknet.us)
