Skip to content

Execute a required reboot, with Ansible (Debian/Ubuntu)

After updating Linux packages, it sometimes is required to reboot the host. Debian and Ubuntu provide this information by the presence of a special file: /var/run/reboot-required. Ansible makes it easy to reboot a host, but there are a few aspects which need attention.

 

First of all, you must ensure that this task runs last in your playbook. Else subsequent tasks will not be executed. But you also must ensure that Ansible can finish the current task, and that the reboot is not killing the network connection while the play is still running. And finally it would be nice to get feedback if the host comes up again.

The first item is easy to handle, just make the reboot task the last one in your playbook. The second one, making sure that the task can finish, can be handled by delaying the reboot.

Unfortunately the "shutdown" command in Linux can only handle delays in minute steps - and we don't want to wait a minute for the reboot. On the other hand, the Ansible task must finish. Therefore, send the entire task into the background:

( /bin/sleep 5 ; shutdown -r now "Ansible updates triggered" ) &

This will wait 5 seconds, and then immediately reboot the host. The outer brackets will make sure that the entire task is sent to the background, using the &. The full play:

# make this the last play, it might reboot the server
- hosts: all
  become: yes
  tasks:
    - name: Reboot required
      shell: ( /bin/sleep 5 ; shutdown -r now "Ansible updates triggered" ) &
            removes=/var/run/reboot-required
      async: 30
      poll: 0
      ignore_errors: true
      notify:
        - waiting for server to come back

This play starts a handler, which will wait a few seconds (to make sure the reboot is in progress) and then wait for the host to come back online:

  handlers:
    - name: waiting for server to come back
      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 (Debian/Ubuntu) at del.icio.us
  • Facebook
  • Google Bookmarks
  • FriendFeed
  • Digg Execute a required reboot, with Ansible (Debian/Ubuntu)
  • Bloglines Execute a required reboot, with Ansible (Debian/Ubuntu)
  • Technorati Execute a required reboot, with Ansible (Debian/Ubuntu)
  • Fark this: Execute a required reboot, with Ansible (Debian/Ubuntu)
  • Bookmark Execute a required reboot, with Ansible (Debian/Ubuntu) at YahooMyWeb
  • Bookmark Execute a required reboot, with Ansible (Debian/Ubuntu) at Furl.net
  • Bookmark Execute a required reboot, with Ansible (Debian/Ubuntu) at reddit.com
  • Bookmark Execute a required reboot, with Ansible (Debian/Ubuntu) at blinklist.com
  • Bookmark Execute a required reboot, with Ansible (Debian/Ubuntu) at Spurl.net
  • Bookmark Execute a required reboot, with Ansible (Debian/Ubuntu) at Simpy.com
  • Bookmark Execute a required reboot, with Ansible (Debian/Ubuntu) at blogmarks
  • Bookmark Execute a required reboot, with Ansible (Debian/Ubuntu) with wists
  • wong it!
  • Bookmark using any bookmark manager!
  • Stumble It!
  • Identi.ca

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

Frits Hoogland on :

Please mind behaviour with this changes around ansible version 1.9 or so. However, I found consistent detecting of up and down to work best with a sleep and a reboot executed a synchronously, then wait for port 22 to come down, and port 22 to come back up.
Comments ()

Andreas Scherbaum on :

I'm using 2.0, therefore that should not be a problem for me. As you can see, I'm using both the sleep and the async to make this work.
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