Skip to main content

How to automate system reboots using the Ansible reboot module

Reboots are sometimes necessary. Automate the process by using the Ansible reboot module.
How to automate system reboots using the Ansible reboot module

Photo by Tiger Lily from Pexels

In this article, I cover how to use Ansible to reboot a Linux or Windows machine efficiently. Ansible reboot module was introduced in Ansible 2.7 (2018), and now this module is part of ansible-base and included in all Ansible installations. Please note, the reboot module needs to be executed from the Ansible controller node and will not work with nodes such as localhost.

Why do you need to reboot a managed node?

Rebooting a Linux operating system is very rare. Most of the time, we can achieve the desired result by restarting or reloading individual services after configuration changes or package updates. But in some cases, like patching or a system upgrade, a reboot operation is required to complete the full workflow and effect. If you are using Ansible for management, you need to include this reboot task to accomplish full automation.

Automated reboots are very easy in Ansible as you have multiple methods to achieve a system reboot task.


Ensure the remote_user—which you are using for Ansible to login to the managed node—has sudo access and the permission to reboot the node.

The good old method – Reboot command and wait

In the past, we used to reboot a managed node using Ansible by a two-step method. (There are many ways, but this method is a common practice):

  1. We will trigger a reboot command using the shell module.
  2. We will use a wait_for_connection module to check the node coming back online after a reboot.

Let me show you an example.

The first task is a simple shell module to reboot the machine, and Ansible will disconnect from the node. In the next task, Ansible will wait for the node to come back online but with a timeout of 300 seconds or five minutes.

- name: Reboot the machine
  shell: "sleep 5 && reboot"

  async: 1
  poll: 0

- name: Wait for the machine to come back online
    connect_timeout: 60
    sleep: 5
    delay: 5
    timeout: 300

Pretty simple right? Hold on, there is a better and more efficient method with the Ansible reboot module, and you can achieve the goal with a single task.

[ You might also enjoy reading: Introducing the new Ansible Automation Hub ]

The new and efficient method – reboot using the Ansible reboot module

Ansible reboot module was introduced in Ansible 2.7 (in 2018), and now this module is part of ansible-base and included in all Ansible installations. The pre-requisites are the same, but you just need a simple task with a reboot module. Ansible reboot module will take care of rebooting the systems or managed node, wait for the system to go down, come back online, and respond to commands.

You can simply reboot the machine without any additional options.

- name: Reboot the machine with all defaults options

There are options like reboot_timeout for waiting for a timeout or pre_reboot_delay, which will wait until the specified number of seconds before reboot. Refer to the Ansible Reboot Module documentation for details.

So here is our playbook with a reboot task. You can see it is very simple and easy to manage.

- name: Linux Reboot Demo
  hosts: rhel7-base
  gather_facts: no
  remote_user: devops
  become: true
    - name: Reboot the machine (Wait for 5 min)
        reboot_timeout: 300

Important notes

If your remote_user is configured with a sudo password, make sure you add -K argument and enter the sudo password. Here is an example:

$ ansible-playbook ansible-os-utils/linux-reboot.yaml -K

If you are using Ansible Tower, add the sudo password in the Credentials interface as shown here:

Ansible Tower credentials user interface
Ansible Tower credentials

Let's test the playbook.

$ ansible-playbook ansible-os-utils/linux-reboot.yaml -K
BECOME password: 
PLAY [Linux Reboot Demo] ************************************************************************************
TASK [Reboot the machine (Wait for 5 min)] ******************************************************************
changed: [rhel7-base.lab.local]
PLAY RECAP **************************************************************************************************
rhel7-base.lab.local       : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0    

Rebooting a Windows machine

What about rebooting Windows machines? As you know, you can manage Windows machines using Ansible, and this reboot task will be needed as part of your automation workflow.

The good news is that, like any other Windows alternatives, there is a dedicated Ansible win_reboot module to handle this task. Here is what the setting looks like:

- name: Reboot a Windows Machine
    reboot_timeout: 360

Refer to the Ansible win_reboot module documentation for details.

[ A free guide from Red Hat: 5 steps to automate your business. ] 

Wrap up

Using reboot or win_reboot module in Ansible is quite easy, and you already learned this. Make sure you have proper pre-reboot (reboot only if needed) and post-reboot (to verify the service or configuration after reboot) tasks in your Ansible playbook as required.


Check out these related articles on Enable Sysadmin

Introduction to Ansible Tower
Not sure whether you need Red Hat Ansible or Red Hat Ansible Tower? Read on, and learn how to get started with Ansible Tower as well.
Topics:   Linux   Linux administration   Ansible   Automation  
Author’s photo

Gineesh Madapparambath

Gineesh has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerization (OpenShift and Kubernetes), and Infrastructure as Code (Terraform). More about me

Red Hat Summit 2022: On Demand

Get the latest on Ansible, Red Hat Enterprise Linux, OpenShift, and more from our virtual event on demand.

Related Content