A while ago I blogged about executing a reboot using Ansible on Debian-based operating systems. That is necessary after certain updates, as example after installing a new kernel. Turns out that things are very easy on Debian, compared to Red Hat based systems (Red Hat and CentOS in my case).
First of all, there is no clear indicator if a reboot is required. People work around this problem by creating overly complicated scripts. The
needs-restarting tool in newer versions provides the -r option to indicate if a reboot is required. But the CentOS 7 I’ve just installed does not come with this version.
I have no patience for complicated solutions when things could be easy. Red Hat is 16 years on the market, and the need for occasionally reboots is even older than that. After such a long time they could have figured out a good way to indicate a required reboot. Therefore my Ansible playbook installs the updated version of needs-restarting in
The step to identify if a reboot is required is split in two parts: 1) run
needs-restarting -r and save the result and 2) execute the
reboot based on the return code.
reboot_required variable stores the result of the command, and the return code is used in the second task. A return
code == 1 indicates that a restart is required.
The handler to wait for the server to come back online is the same as I’m using with Debian: