Updated April 12, 2023
Introduction to Ansible Reboot
Ansible provides a plenty of modules which are direct replacement of our Unix/Linux commands, but can work in better way than using those similar commands in scripts. One such module is Ansible reboot module. This module is a replacement of reboot command on Linux/Unix systems but can work in better way. This means, we can reboot the remote target machines with some checks and additional delays pre and post reboot. For Microsoft Windows environments, we have similar module named as win_reboot.
What is Ansible Reboot?
This module basically is used for rebooting a machine, wait for successful stopping and starting then respond to test commands, to confirm reboot was successful. On Linux/Unix machines, it basically invokes shutdown command available under one of the paths /sbin, /usr/sbin, /usr/local/sbin. The path of shutdown command can be provided manually by parameter path.
How does Ansible Reboot work?
Like any other Ansible Module, Ansible reboot module is equipped with a set of parameters and acceptable values. Some of these parameters have default values which will be realized to your playbook even if you do not mention the related parameter in playbook. So these become more important to know.
Given below is a list of all available parameters and related acceptable or default values.
- test_command: This parameter is used to give the command on rebooted hosts after reboot task and expect success to confirm whether the machine is ready for next tasks in playbook. Default command is “whoami”.
- search_paths: This is to give the locations to search for shutdown command on remote target machines. When this is used then PATH environment variable will be Default is /sbin, /usr/sbin and /local/usr/sbin.
- reboot_timeout: This time is in seconds and to specify the time to wait for the machine to reboot and respond to a test command. This will be evaluated separately for verification and test command success. Which means maximum amount of time for execution is twice of this value. By default it is set to 600 seconds.
- pre_reboot_delay: Time to wait before reboot, this will be set as parameter to reboot On *nix based machines, this is calculated on minutes basis, means seconds converted to minutes and rounded down. If less than 1 minute then it takes as 0. Default is 0.
- post_reboot_delay: This is time in seconds to wait, after reboot. Before attempting to validate the system reboot. This is useful if you want to settle down something after reboot. Default is 0.
- connect_timeout: The amount of time in seconds to wait for successful connection after reboot, before trying again.
- msg: Default message is “Reboot Initiated by Ansible”. This is to specify the message to display to working users before rebooting.
Given below are the return values:
- elapsed: The time elapsed waiting for reboot.
- rebooted: True if the machine was successfully rebooted.
Examples of Ansible Reboot
Given below are the examples mentioned:
Here we have one Ansible controller node named as ansible-controller. As target nodes, we have two remote machines. First machine is a Red Hat Enterprise Linux machine named as host-one and second machine is an Ubuntu machine named as host-two. We will run our playbooks on Ansible controller machine and make changes on remote target machines.
Example #1
In this example, we have a playbook like below where we are simply rebooting the target machines.
Code:
---
- hosts:
all
tasks:
- name: Here we are rebooting the remote machines. reboot:
Running this playbook like below with some verbosity:
ansible-playbook ansible_reboot.yaml -v
In the output we can see all the outputs and returned values to figure out how much time it took and whether reboot was successful.
ansible-playbook --syntax-check ansible_reboot.yaml
Output:
Example #2
In this example, we have a playbook like below where we are rebooting the target machines with some pre and post delay and then have a command to test the server is up and ready for next tasks. Here we have used test command as “uptime” instead of default “whoami”.
Code:
---
-hosts:
all
tasks:
-name: Here we are checking the time on remote machines before shell: date
-name: Here we are rebooting with some reboot:
pre_reboot_delay: 10
post_reboot_delay: 10 test_command: uptime
-name: Here we are checking the time on remote machines after shell: date
Running this playbook like below with some verbosity:
ansible-playbook ansible_reboot_with_parameters.yaml -v
In the output we can see all the outputs and returned values to figure out how much time it took with the output of test command.
Output:
Example #3
In this example, we have a playbook like below where we are rebooting the target machines with a test command to create a sample file on remote machines. This is to verify that test commands got executed for verification of reboot.
Code:
---
-hosts:
all
tasks:
-name: Here we are checking the time on remote machines before shell: date
-name: Here we are rebooting with some reboot:
test_command: touch /var/tmp/reboot_1.txt
-name: Here we are checking the time on remote machines after shell: "ls -l /var/tmp/reboot_1.txt; date"
Now running this playbook like below:
ansible-playbook ansible_reboot_with_parameters.yaml -v
In the below output we can see all the time captured before and after reboot and also the file created as reboot verification.
Output:
Conclusion
As we saw in this article, Ansible Reboot is a module which you will need, for sure, in your daily administration or change management tasks. This module provides you ability to work with reboots smartly. But as this module will reboot the remote machines which can lead to downtime or outage. So you must only use this in planned activities. So learn it first and then use it.
Recommended Articles
This is a guide to Ansible Reboot. Here we discuss what is ansible reboot? how does ansible reboot work? and examples. You may also have a look at the following articles to learn more –