Last Updated: September 09, 2019
·
18.25K
· Lorin Hochstein

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

4 Responses
Add your response

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

over 1 year ago ·

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

over 1 year ago ·

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 ·

Nice... that is a lot more elegant.

over 1 year ago ·