Rebooting and waiting

897 views
Skip to first unread message

Francisco Lopes

unread,
May 27, 2016, 11:10:55 AM5/27/16
to Ansible Project
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:

- name: reboot system if required
  command: removes=/var/run/reboot-required sleep 1 && shutdown -r now 'Rebooting to complete system upgrade'
  async: 1
  poll: 0
  ignore_errors: true
  register: reboot

- name: wait for server to come back (if restarted)
  local_action: wait_for host={{ inventory_hostname }} state=started delay=1 timeout=300
  when: reboot.unreachable is defined and reboot.unreachable

But this is not rebooting and the local_action is being skipped. I have a condition here because if a reboot wasn't necessary, I don't want to wait for the delay.

Thanks in advance.

Regards,
Francisco

Kai Stian Olstad

unread,
May 27, 2016, 12:49:27 PM5/27/16
to ansible...@googlegroups.com
On 27. May 2016 17:10, Francisco Lopes wrote:
> - name: reboot system if required
> command: removes=/var/run/reboot-required sleep 1 && shutdown -r
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)."

--
Kai Stian Olstad

Johannes Kastl

unread,
May 27, 2016, 1:30:43 PM5/27/16
to ansible...@googlegroups.com
On 27.05.16 17:10 Francisco Lopes wrote:
> 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

...works for me.

Johannes


signature.asc

Francisco Lopes

unread,
May 27, 2016, 2:24:42 PM5/27/16
to ansible...@googlegroups.com
How exactly are you using it?

- name: reboot system if required
  shell: sleep 2 && shutdown -r now 'Rebooting to complete system upgrade'

  async: 1
  poll: 0
  ignore_errors: true

- name: wait for server to come back
  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



Johannes


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/57488435.3050607%40ojkastl.de.
For more options, visit https://groups.google.com/d/optout.

Francisco Lopes

unread,
May 27, 2016, 2:40:57 PM5/27/16
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Johannes Kastl

unread,
May 27, 2016, 2:46:21 PM5/27/16
to ansible...@googlegroups.com
On 27.05.16 20:24 Francisco Lopes wrote:

> - name: wait for server to come back
> 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.

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

signature.asc

Francisco Lopes

unread,
May 27, 2016, 3:29:10 PM5/27/16
to ansible...@googlegroups.com
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


Johannes

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Kai Stian Olstad

unread,
May 27, 2016, 3:40:02 PM5/27/16
to ansible...@googlegroups.com
On 27. mai 2016 21:28, Francisco Lopes wrote:
> 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?

Not tested but this should work

- stat: /var/run/reboot-required
register: result_reboot

- name: reboot system if required
shell: sleep 2 && shutdown -r now
async: 1
poll: 0
ignore_errors: true
when: result_reboot.stat.exists == True

- name: wait for server to come back
local_action: wait_for host={{ inventory_hostname }} state=started
delay=5 timeout=30
when: result_reboot.stat.exists == True

--
Kai Stian Olstad

Francisco Lopes

unread,
May 27, 2016, 4:07:30 PM5/27/16
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Johannes Kastl

unread,
May 27, 2016, 4:08:47 PM5/27/16
to ansible...@googlegroups.com
On 27.05.16 21:28 Francisco Lopes wrote:

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

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

Johannes

signature.asc
Reply all
Reply to author
Forward
0 new messages