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
Written by Lorin Hochstein
Related protips
4 Responses

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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#