6Oct

Using Reboot Module in Ansible 2.7

For a while, rebooting a Linux machine using Ansible has been done primarily using a combination of a reboot shell command module and a wait_for clause to pause execution of other tasks until that machine has came back up. This is also the method that is being taught at the current available revision of DO407 Automation with Ansible course by Red Hat (based on Ansible 2.3). Using this method can be done as follows:

---
- name: Reboot and wait until the server is up
  hosts: server1
  tasks:

    - name: reboot machine
      shell: sleep 2; shutdown -r now "Ansible triggered reboot"
      async: 1
      poll: 0
      ignore_errors: true

    - name: Wait for server to come back
      wait_for:
        host: "{{ inventory_hostname }}"
        state: started
        delay: 30
        timeout: 300
        port: 22
      delegate_to: localhost

New in Ansible 2.7: reboot module

a few days ago, Ansible 2.7 has been released with a TON of additions, including a new reboot module, which is great in how it replaces the aforementioned method.

Rebooting in its simplest form can be done using this tiny task:

---
- name: reboot the machine using default parameters
  reboot:

That’s it! this will do what we have been doing previously just as good. The module also provides the time it took to reboot the machine. It also has options to specify a reboot message, pre and post reboot delays, as well as which command to run to verify reboot is completed given that its return code is zero. This is replacing the function of wait_for if you need a specific slow service to come up.

Is it over for the wait_for module?

Absolutely not! While the reboot module replaces the old method for rebooting, wait_for is still here for your other use cases that are not based on a reboot. If you use Ansible to manage Load Balancer Pool Members for example, you would still need it to verify connection drain, or if a member is ready to be added to the pool.

There is a very good explanation of how the module works by its developer on Ansible Blog, give it a read. Also, check the reboot and wait_for module documentation for list of all options and some use cases.

 

Share this Story

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Written with love ♥