0nckqg
Last Updated: March 02, 2016
·
6.85K
· lorin
Me pixel

Ansible: Reboot Ubuntu if needed

Simple way to check if a recent package install/upgrade requires a reboot on Ubuntu.

(Note: Alexander Dupuy has a more elegant solution in the comments below)

tasks:
   - name: check if a reboot is required
     shell: "[ -f /var/run/reboot-required ]"
     failed_when: False
     register: reboot_required
     changed_when: reboot_required.rc == 0
     notify: reboot

handlers:
   - name: reboot
     command: /sbin/reboot
Say Thanks
Respond

4 Responses
Add your response

13876
Profiel foto square

Should this not also need a "notify: reboot" ?

over 1 year ago ·
14165
Me pixel

Whoops, you're absolutely right. Added the missing notify.

over 1 year ago ·
15006
Ea113a3a40948586550391a9d60ddde9

I just use the "removes=" option for Ansible "command" module to selectively run the reboot - much simpler, and no handler needed.

tasks:
- name: Reboot system if required
  command: shutdown -r now 'Rebooting to complete system upgrade'
            removes=/var/run/reboot-required
over 1 year ago ·
15270
Me pixel

Nice... that is a lot more elegant.

over 1 year ago ·
Filed Under