Ansible Playbook: Reboot Servers

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

---
- hosts: all
  tasks:
    - name: check if reboot is required
      stat: path=/var/run/reboot-required
      register: reboot_file
    - name: reboot host
      reboot:
      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.