Skip to content

Execute a required reboot, with Ansible (CentOS/Red Hat)

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 /root.

- name: install own version of 'needs-restarting'
  copy: src=needs-restarting.py dest=/root/needs-restarting.py mode=0700
  when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'

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.

- name: Reboot required (Red Hat) - Step 1
  command: /root/needs-restarting.py -r
  register: reboot_required
  ignore_errors: True
  changed_when: False
  when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'

- name: Reboot required (Red Hat) - Step 2
  shell: ( /bin/sleep 5 ; shutdown -r now "Ansible updates triggered" ) &
  async: 30
  poll: 0
  ignore_errors: true
  notify:
    - waiting for server to come back after reboot
  when: (ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux') and reboot_required.rc == 1

The "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:

- name: waiting for server to come back after reboot
  local_action: wait_for host={{ inventory_hostname }} port=22 state=started delay=10 timeout=60
  become: no

 

  • Twitter
  • Bookmark Execute a required reboot, with Ansible (CentOS/Red Hat) at del.icio.us
  • Facebook
  • Google Bookmarks
  • FriendFeed
  • Digg Execute a required reboot, with Ansible (CentOS/Red Hat)
  • Bloglines Execute a required reboot, with Ansible (CentOS/Red Hat)
  • Technorati Execute a required reboot, with Ansible (CentOS/Red Hat)
  • Fark this: Execute a required reboot, with Ansible (CentOS/Red Hat)
  • Bookmark Execute a required reboot, with Ansible (CentOS/Red Hat) at YahooMyWeb
  • Bookmark Execute a required reboot, with Ansible (CentOS/Red Hat) at Furl.net
  • Bookmark Execute a required reboot, with Ansible (CentOS/Red Hat) at reddit.com
  • Bookmark Execute a required reboot, with Ansible (CentOS/Red Hat) at blinklist.com
  • Bookmark Execute a required reboot, with Ansible (CentOS/Red Hat) at Spurl.net
  • Bookmark Execute a required reboot, with Ansible (CentOS/Red Hat) at Simpy.com
  • Bookmark Execute a required reboot, with Ansible (CentOS/Red Hat) at blogmarks
  • Bookmark Execute a required reboot, with Ansible (CentOS/Red Hat) with wists
  • wong it!
  • Bookmark using any bookmark manager!
  • Stumble It!
  • Identi.ca

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

Waqas on :

Hi, although this blog post is too old but I want to thank you for the idea of using Centos 7 needs-restarting python script for Centos 6. This solved my big problem. Thank you for this.
Comments ()

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
Form options