Highly Opinionated Thoughts on Programming

by Elnur Abdurrakhimov

Rebooting Servers One-by-One With Ansible

May 20, 2014

If you have several nodes, you want to reboot them one by one so that online nodes can still do their jobs. Here’s a playbook that shows one way of doing that in Ansible:

- hosts: all
  serial: 1
    - name: Reboot
      command: /sbin/reboot removes=/var/run/reboot-required

    - name: Wait for the server to reboot
      local_action: wait_for host= port=22

serial: 1 makes the playbook execute all the tasks for a single server before moving to the next one.

Since I use Ubuntu on servers, I can check for existence of the /var/run/reboot-required file to know if a reboot is required. Maybe it works for some other distros, but I haven’t checked that.

local_action is needed here to ping the server to come online from the host machine because the target server will not be online to do the required check.

wait_for is a module that waits for some condition to come true.

Here I’m checking for the SSH port to be available. The wait_for module supports other kinds of checks you could use in your particular situation.

Note that I’m using the inventory_hostname variable. The reason for that is that we need to use the hostname we use to connect to the server from the host machine. ansible_hostname won’t work here because it refers to the hostname set on the server itself.

© Elnur Abdurrakhimov