# Reliably wait\_for SSH connection to go up

**URL:** <https://forum.ansible.com/t/reliably-wait-for-ssh-connection-to-go-up/14974>\
**Category:** Ansible Project\
**Created:** [April 9, 2014, 3:26am UTC](https://forum.ansible.com/t/reliably-wait-for-ssh-connection-to-go-up/14974 "2014-04-09T03:26:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anatoly\_techtonik](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anatoly\_techtonik](https://forum.ansible.com/u/anatoly_techtonik)\
**Post date:** [April 9, 2014, 3:26am UTC](https://forum.ansible.com/t/reliably-wait-for-ssh-connection-to-go-up/14974/1 "2014-04-09T03:26:15Z")

</div>



---

<div class="post-metadata">

**Author:** ![anatoly\_techtonik](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anatoly\_techtonik](https://forum.ansible.com/u/anatoly_techtonik)\
**Post date:** [April 9, 2014, 3:49am UTC](https://forum.ansible.com/t/reliably-wait-for-ssh-connection-to-go-up/14974/2 "2014-04-09T03:49:56Z")

</div>

I also tried this one - doesn’t work.

- name: waiting for machine to come back  
local\_action: wait\_for host={{ ansible\_ssh\_host }} port={{ ansible\_ssh\_port }} delay=30 timeout=180

#- pause: seconds=5

- command: echo “X”  
register: status  
ignore\_errors: True  
until: status != “X”  
retries: 5  
delay: 10

Same {‘msg’: ‘FAILED: Error reading SSH protocol banner’, ‘failed’: True}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 9, 2014, 2:12pm UTC](https://forum.ansible.com/t/reliably-wait-for-ssh-connection-to-go-up/14974/3 "2014-04-09T14:12:43Z")

</div>

Sounds like the port was open but the SSH agent wasn’t ready to receive traffic yet.

I’d suggest inserting a small call to “pause: seconds=5” after the wait\_for and see if that resolves the issue.

It usually does.

---

<div class="post-metadata">

**Author:** ![anatoly\_techtonik](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anatoly\_techtonik](https://forum.ansible.com/u/anatoly_techtonik)\
**Post date:** [April 9, 2014, 3:44pm UTC](https://forum.ansible.com/t/reliably-wait-for-ssh-connection-to-go-up/14974/4 "2014-04-09T15:44:30Z")

</div>

Port is open, but SSH agent is not ready to receive traffic.

pause: seconds=5 command already was there, doesn't help.  
increasing to 30 sometimes helps and sometimes not - depending  
on the load on virtual machine. I hoped that there is a way to set  
flexible wait time from 30 seconds up to 15 minutes. Setting pause  
to 15 minutes will annoy humans and to 30 seconds will likely to  
break automation at some point.

---

<div class="post-metadata">

**Author:** ![Scott\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Scott\_Anderson](https://forum.ansible.com/u/Scott_Anderson)\
**Post date:** [April 9, 2014, 5:51pm UTC](https://forum.ansible.com/t/reliably-wait-for-ssh-connection-to-go-up/14974/5 "2014-04-09T17:51:57Z")

</div>

I’m having exactly this problem today. The script already takes 2 minutes to run so I am loathe to add more time to a wait, and at some point that will be insufficient again.

It would be nice to have a wait that specifically checks to make sure a ssh connection works instead of depending on a port check only.

-scott

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [April 9, 2014, 10:51pm UTC](https://forum.ansible.com/t/reliably-wait-for-ssh-connection-to-go-up/14974/6 "2014-04-09T22:51:43Z")

</div>

Since wait\_for isn’t work for you, might I suggest as a hacky workaround utilizing a do-until loop?

docs: [http://docs.ansible.com/playbooks\_loops.html#do-until-loops](http://docs.ansible.com/playbooks_loops.html#do-until-loops)

Basically you would define a local action that would attempt to ssh to the inventory\_hostname and run echo foo. If it returns successfully it should mean that ssh is fully ready. It will keep trying until it gets a return code that indicates a success or fails 5 times with a 10 second interval.

Here’s how it would look:

- hosts: 10.42.0.6  
gather\_facts: no  
tasks:
- local\_action: command ssh vagrant@{{ inventory\_hostname }} echo foo  
register: result  
until: result.rc == 0  
retries: 20  
delay: 10

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 15, 2014, 8:47pm UTC](https://forum.ansible.com/t/reliably-wait-for-ssh-connection-to-go-up/14974/7 "2014-04-15T20:47:44Z")

</div>

I really don’t like the idea of executing SSH inside of ansible directly because it wouldn’t work with any of the --ask-pass options. (But should be fine with ssh-agent).

Anyway, haven’t seen this myself. Having SSH not functional after 30 seconds of the port being open seems quite odd to me to be honest.
