# wait\_for on reboot doesn't seem to be waiting.

**URL:** <https://forum.ansible.com/t/wait-for-on-reboot-doesnt-seem-to-be-waiting/28139>\
**Category:** Ansible Project\
**Created:** [July 24, 2018, 5:53pm UTC](https://forum.ansible.com/t/wait-for-on-reboot-doesnt-seem-to-be-waiting/28139 "2018-07-24T17:53:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [July 24, 2018, 5:53pm UTC](https://forum.ansible.com/t/wait-for-on-reboot-doesnt-seem-to-be-waiting/28139/1 "2018-07-24T17:53:42Z")

</div>

Hi all,

I’m trying to write a playbook that patches and reboots a server if necessary to apply kernel updates, etc. When I run it, it reboots and loses the connection instead of applying the **wait\_for** piece.

Any ideas?

Here’s my playbook:

- name: Updating Servers  
hosts: stig-test-lx  
gather\_facts: true

tasks:

- name: Update to latest of all pkgs  
yum:  
name: “\*”  
state: latest

- name: See if needs to reboot to apply kernel  
shell: “LAST\_KERNEL=$(rpm -q --last kernel | awk ‘NR==1{sub(/kernel-/,""); print $1}’); CURRENT\_KERNEL=$(uname -r); if [$LAST\_KERNEL != $CURRENT\_KERNEL]; then echo ‘reboot’; else echo ‘no’; fi”  
ignore\_errors: true  
register: reboot\_hint

- name: Rebooting …  
command: shutdown -r +1 “Rebooting due to kernel updates”  
async: 30  
poll: 0  
ignore\_errors: true  
when: reboot\_hint.stdout.find(“reboot”) != -1

- name: Waiting for system to come back up  
wait\_for:  
host: “{{ (ansible\_ssh\_host|default(ansible\_host))|default(inventory\_hostname) }}”  
port: 22  
search\_regex: OpenSSH  
delay: 10  
timeout: 180

# when: reboot\_hint.stdout.find(“reboot”) != -1

vars:  
ansible\_connection: local

---

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [July 24, 2018, 5:58pm UTC](https://forum.ansible.com/t/wait-for-on-reboot-doesnt-seem-to-be-waiting/28139/2 "2018-07-24T17:58:02Z")

</div>

Note: I commented out the “when” to test the reboot regardless.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 24, 2018, 6:59pm UTC](https://forum.ansible.com/t/wait-for-on-reboot-doesnt-seem-to-be-waiting/28139/3 "2018-07-24T18:59:16Z")

</div>

> Hi all,
> 
> I'm trying to write a playbook that patches and reboots a server if  
> necessary to apply kernel updates, etc. When I run it, it reboots and  
> loses the connection instead of applying the \*wait\_for\* piece.
> 
> Any ideas?
> 
> Here's my playbook:
> 
> - name: Updating Servers  
> &nbsp;&nbsp;hosts: stig-test-lx  
> &nbsp;&nbsp;gather\_facts: true
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- name: Update to latest of all pkgs  
> &nbsp;&nbsp;&nbsp;&nbsp;yum:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "\*"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: latest
> 
> &nbsp;&nbsp;- name: See if needs to reboot to apply kernel  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: "LAST\_KERNEL=$(rpm -q --last kernel | awk  
> 'NR==1{sub(/kernel-/,\"\"); print $1}'); CURRENT\_KERNEL=$(uname -r); if [  
> $LAST\_KERNEL != $CURRENT\_KERNEL ]; then echo 'reboot'; else echo 'no'; fi"  
> &nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: true  
> &nbsp;&nbsp;&nbsp;&nbsp;register: reboot\_hint
> 
> &nbsp;&nbsp;- name: Rebooting ...  
> &nbsp;&nbsp;&nbsp;&nbsp;command: shutdown -r +1 "Rebooting due to kernel updates"  
> &nbsp;&nbsp;&nbsp;&nbsp;async: 30  
> &nbsp;&nbsp;&nbsp;&nbsp;poll: 0  
> &nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: true  
> &nbsp;&nbsp;&nbsp;&nbsp;when: reboot\_hint.stdout.find("reboot") != -1

+1 in shutdown means wait 1 minute before rebooting.

> &nbsp;&nbsp;- name: Waiting for system to come back up  
> &nbsp;&nbsp;&nbsp;&nbsp;wait\_for:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: "{{  
> (ansible\_ssh\_host|default(ansible\_host))|default(inventory\_hostname) }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: 22  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search\_regex: OpenSSH  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delay: 10  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeout: 180  
> # when: reboot\_hint.stdout.find("reboot") != -1  
> &nbsp;&nbsp;&nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_connection: local

Your delay is only 10 seconds, so by the time it checks(after 10s) the machine is still on and port 22 is very much alive.  
So your delay must be at least 60s + the time it takes for the machine to stop ssh.

If you want to do this a little faster you can use this task

- name: Reboot  
&nbsp;&nbsp;&nbsp;shell: sleep 2 && shutdown -r now  
&nbsp;&nbsp;&nbsp;async: 1  
&nbsp;&nbsp;&nbsp;poll: 0

And if you are on Ansible 2.3 or newer I recommend using wait\_for\_connection instead of wait\_for.  
wait\_for\_connection waits until the system is capable/ready to run Ansible code and not just that ssh has started.

---

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [July 24, 2018, 7:04pm UTC](https://forum.ansible.com/t/wait-for-on-reboot-doesnt-seem-to-be-waiting/28139/4 "2018-07-24T19:04:05Z")

</div>

Awesome! Thanks for that little tidbit regarding the wait\_for\_connection! That’s huge!

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [July 24, 2018, 11:28pm UTC](https://forum.ansible.com/t/wait-for-on-reboot-doesnt-seem-to-be-waiting/28139/5 "2018-07-24T23:28:11Z")

</div>

I think you need to increase the poll
