# Rebooting and waiting

**URL:** <https://forum.ansible.com/t/rebooting-and-waiting/21583>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [May 27, 2016, 3:10pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583 "2016-05-27T15:10:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francisco\_Lopes](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Francisco\_Lopes](https://forum.ansible.com/u/Francisco_Lopes)\
**Post date:** [May 27, 2016, 3:10pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583/1 "2016-05-27T15:10:15Z")

</div>

Hello,

How do I execute a reboot (if needed on Ubuntu) and wait for it to return instead of getting the unreachable error?

I’m currently trying to do this based on past posts and links I found:

---

<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:** [May 27, 2016, 4:48pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583/2 "2016-05-27T16:48:56Z")

</div>

now 'Rebooting to complete system upgrade'

From the command module documentation:  
"It will not be processed through the shell, so variables like $HOME and operations like "\<", "\>", "|", and "&" will not work (use the shell module if you need these features)."

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 27, 2016, 5:30pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583/3 "2016-05-27T17:30:29Z")

</div>

> Hello,
> 
> How do I execute a reboot (if needed on Ubuntu) and wait for it to return  
> instead of getting the unreachable error?

This...

> [https://support.ansible.com/hc/en-us/articles/201958037-Reboot-a-server-and-wait-for-it-to-come-back](https://support.ansible.com/hc/en-us/articles/201958037-Reboot-a-server-and-wait-for-it-to-come-back)

...works for me.

Johannes

---

<div class="post-metadata">

**Author:** ![Francisco\_Lopes](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Francisco\_Lopes](https://forum.ansible.com/u/Francisco_Lopes)\
**Post date:** [May 27, 2016, 6:24pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583/4 "2016-05-27T18:24:18Z")

</div>

> This...
> 
> \>  
> [https://support.ansible.com/hc/en-us/articles/201958037-Reboot-a-server-and-wait-for-it-to-come-back](https://support.ansible.com/hc/en-us/articles/201958037-Reboot-a-server-and-wait-for-it-to-come-back)
> 
> ...works for me.

How exactly are you using it?

- name: reboot system if required  
&nbsp;&nbsp;shell: sleep 2 && shutdown -r now 'Rebooting to complete system upgrade'  
&nbsp;&nbsp;async: 1  
&nbsp;&nbsp;poll: 0  
&nbsp;&nbsp;ignore\_errors: true

- name: wait for server to come back  
&nbsp;&nbsp;local\_action: wait\_for host={{ inventory\_hostname }} state=started  
delay=30 timeout=300

This hangs on the waiting task, even though the servers are back online.

Regards,  
Francisco

---

<div class="post-metadata">

**Author:** ![Francisco\_Lopes](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Francisco\_Lopes](https://forum.ansible.com/u/Francisco_Lopes)\
**Post date:** [May 27, 2016, 6:40pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583/5 "2016-05-27T18:40:31Z")

</div>

> From the command module documentation:  
> “It will not be processed through the shell, so variables like $HOME and operations like “\<”, “\>”, “|”, and “&” will not work (use the shell module if you need these features).”

Thanks, I missed that one. I have this now:

- name: reboot system if required  
shell: removes=/var/run/reboot-required sleep 2 && shutdown -r now ‘Rebooting to complete system upgrade’  
async: 1  
poll: 0  
ignore\_errors: true  
register: reboot

- name: wait for server to come back  
local\_action: wait\_for host={{ inventory\_hostname }} state=started delay=2 timeout=300  
when: reboot.unreachable is defined and reboot.unreachable

Still not working though. It successfully execute the reboot, but the wait task is skipped and then I get an unreachable error on the following tasks. Any idea on how to know when a reboot was executed? ☹

Regards,

Francisco

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 27, 2016, 6:45pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583/6 "2016-05-27T18:45:59Z")

</div>

It waits for seconds before starting to test, and then it tests for 5  
minutes, before it fails.

Settings this to 'delay=15 timeout=30‘ should speed things up, because  
it fails after 45s if the host is not reachable. Also, setting a  
smaller delay works, if your host is rebooting that quickly. If it  
needs some minutes to reboot, you obviously need other values...

Johannes

---

<div class="post-metadata">

**Author:** ![Francisco\_Lopes](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Francisco\_Lopes](https://forum.ansible.com/u/Francisco_Lopes)\
**Post date:** [May 27, 2016, 7:28pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583/7 "2016-05-27T19:28:44Z")

</div>

> It waits for seconds before starting to test, and then it tests for 5  
> minutes, before it fails.
> 
> Settings this to 'delay=15 timeout=30‘ should speed things up, because  
> it fails after 45s if the host is not reachable. Also, setting a  
> smaller delay works, if your host is rebooting that quickly. If it  
> needs some minutes to reboot, you obviously need other values…

I see. I guess a found a good default for my case, which would be:

- name: reboot system if required  
shell: removes=/var/run/reboot-required sleep 2 && shutdown -r now  
async: 1  
poll: 0  
ignore\_errors: true

- name: wait for server to come back  
local\_action: wait\_for host={{ inventory\_hostname }} state=started delay=5 timeout=30

So right now it correctly waits for the server to come back.

Is it possible to add a condition on the wait task in order to skip it if a reboot was not required?

Regards,

Francisco

---

<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:** [May 27, 2016, 7:39pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583/8 "2016-05-27T19:39:52Z")

</div>

Not tested but this should work

- stat: /var/run/reboot-required  
&nbsp;&nbsp;&nbsp;register: result\_reboot

- name: reboot system if required  
&nbsp;&nbsp;&nbsp;shell: sleep 2 && shutdown -r now  
&nbsp;&nbsp;&nbsp;async: 1  
&nbsp;&nbsp;&nbsp;poll: 0  
&nbsp;&nbsp;&nbsp;ignore\_errors: true  
&nbsp;&nbsp;&nbsp;when: result\_reboot.stat.exists == True

- name: wait for server to come back  
&nbsp;&nbsp;&nbsp;local\_action: wait\_for host={{ inventory\_hostname }} state=started delay=5 timeout=30  
&nbsp;&nbsp;&nbsp;when: result\_reboot.stat.exists == True

---

<div class="post-metadata">

**Author:** ![Francisco\_Lopes](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Francisco\_Lopes](https://forum.ansible.com/u/Francisco_Lopes)\
**Post date:** [May 27, 2016, 8:07pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583/9 "2016-05-27T20:07:05Z")

</div>

That was almost right, it worked now using this:

- name: verify if a reboot is required after upgrade  
stat: path=/var/run/reboot-required  
register: reboot\_required

- name: reboot system  
shell: sleep 2 && shutdown -r now  
async: 1  
poll: 0  
ignore\_errors: true  
when: reboot\_required.stat.exists

- name: wait for server to come back  
local\_action: wait\_for host={{ inventory\_hostname }} state=started delay=5 timeout=30  
when: reboot\_required.stat.exists

Thank you! I was trying to do it using removes, register and async\_status, that was surely simpler. 🙂

Regards,

Francisco

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 27, 2016, 8:08pm UTC](https://forum.ansible.com/t/rebooting-and-waiting/21583/10 "2016-05-27T20:08:06Z")

</div>

Put it into a handler instead of as a task. Notify the handler when  
changes occur.

[https://docs.ansible.com/ansible/playbooks\_intro.html#handlers-running-operations-on-change](https://docs.ansible.com/ansible/playbooks_intro.html#handlers-running-operations-on-change)

Johannes
