Ansible

Ansible Reboot

Rebooting essentially means restarting the system you are using. As we all know, while using any computer, there are many reasons you might want to restart or reboot it.

Rebooting allows your system to reset. The temporary memory gets cleared and the system starts again. Rebooting can be done either through hardware or software. In this article, we will be discussing how you can reboot your computer through Ansible.

As you may know, Ansible can be used to control your system and its resources. So, Ansible can be used to control the computer on which it is installed. So, among carrying out other basic functions, you can also use it to reboot your system. For this, you can use the Ansible reboot module.

Why Reboot?

Rebooting is prompted when you install new software or your operating system wants to update itself. Secondly, if your computer stops responding, the go-to step would be to reboot your system.

Rebooting allows your computer to stop any software that is not working properly and close it so it can return to normal. In other words, rebooting is similar to having a safe exit in case of emergencies.

The Reboot Module and its Parameters

The reboot module and command are different for users of Windows and Linux. The parameters being passed would be different for each of them. We will be discussing the reboot module for both operating systems.

Ansible Reboot in Linux

These are the different parameters when you use the reboot in Linux.

Boot_time_command: this parameter tells the user the date and time of the last time the system was rebooted.

Connect_timeout: this parameter is the time that will be taken for the connection to be made with the host machine after rebooting.

Msg: this parameter generates a custom message when Ansible initiates the rebooting procedure.

Post_reboot_delay: using this parameter, we can specify the time that will be taken for establishing a connection with the host system after rebooting.

Pre_reboot_delay: using the pre_reboot_delay parameter, we can specify the system to wait for “x” seconds before it attempts to reboot.

Reboot_command: this is the main command to tell the system to reboot itself. The different parameters being mentioned in the list are used with this command.

Reboot_timeout: this is the time the Ansible machine takes to respond to the reboot command and reboot.

Test_command: with this parameter, we can specify a command that Ansible will run after rebooting. This is used for testing the system to check whether it can accept further commands or not.

These are the different parameters available with the reboot module. Following, are the return values provided by Ansible on successful rebooting.

  • Elapsed: the keyword elapsed tells us the time the system took to reboot itself.
  • Rebooted: this is a Boolean keyword that returns “true” on successful rebooting.

Ansible Reboot in Windows

These are the parameters available for the reboot module in Windows.

Boot_time_command: it is the same parameter as the one available in the Linux reboot module. By passing this as an argument, we get the date and time of the last system reboot.

Connect_timeout: this parameter tells the maximum amount of time the system will wait for establishing a TCP connection to the WinRM endpoint before trying again.

Msg: similar to the one mentioned in the Linux reboot module, it is used to give a message after a successful reboot.

Post_reboot_delay: this parameter is to tell the system to wait for a certain period of time after rebooting before it checks whether the system was successfully rebooted or not.

Pre_reboot_delay: this parameter is similar to the one used in Linux. It tells the system to wait a certain period of time before it attempts to reboot itself.

Reboot_timeout: this parameter specifies the time the system will take before Ansible attempts to reestablish a connection with the hosts.

Test_command: it is an arbitrary command that you specify with the reboot module. It executes itself after rebooting as a test to check whether the system is ready or not.

As you can see, the parameters are pretty much the same but less in number as compared to the ones available with Linux. Next, we look at the return values.

  • Elapsed: the keyword “elapsed” tells us the time the system took to reboot itself.
  • Rebooted: this is a Boolean keyword that returns “true” on successful rebooting.

Examples

- name: Reboot the machine with all default parameters
    reboot:

Now, for running the playbook, type the following command into the shell:

ansible-playbook testbook.yml

This command will simply reboot your system with all the default settings of the parameters.

- name: Rebooting with reboot_timeout parameter.
     reboot:
      reboot_timeout: 3600

Afterward, run the following command on the Linux terminal again:

ansible-playbook testbook.yml

In this command, we have specified to the system that it has to take 3600 seconds before it lets Ansible establish a connection with the servers again. This can be used in case the computer had a lot of updates to install and you want it to settle down before Ansible runs again.

Conclusion

In this article, we looked at what is the Ansible reboot module. We established the reasons why a user might reboot his system and then look at the parameters available with the reboot module for both Linux and Windows. In the end, we looked at two examples that showed how you reboot and one of its parameters (reboot_timeout) can be used in Ansible.

Rebooting can prove to be necessary at certain points and if you want to do it using Ansible reboot, hopefully, this article would be able to guide you on how to do it.

About the author

Zeeman Memon

Hi there! I'm a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.