Rebooting and waiting

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:

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)."

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

This...

>
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
  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

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? :frowning:

Regards,

Francisco

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

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

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

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. :slight_smile:

Regards,

Francisco

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