Ansible Playbook: Reboot Servers

Annika Backstrom
in Technology, on 8 October 2018. It is tagged #ansible and #Linux.

Here's a short Ansible playbook that reboots servers that need a reboot.

- hosts: all
    - name: check if reboot is required
      stat: path=/var/run/reboot-required
      register: reboot_file
    - name: reboot host
      when: reboot_file.stat.exists == true

My hosts are configured to auto-install security updates. Many times, when I log into a host, I'll get a notification that a reboot is required. This playbook lets me broadcast a conditional reboot to all my hosts.

The above playbook requires the reboot module added in Ansible 2.7. The command module could be used on earlier hosts to manually issue a reboot.