Subscribe to the feed

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.

Pre-requisites

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
  wait_for_connection:
    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
  reboot:

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
 
  tasks:
    - name: Reboot the machine (Wait for 5 min)
      reboot:
        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

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
  ansible.windows.win_reboot:
    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.

References:


About the author

Gineesh Madapparambath is a Platform & DevOps Consultant at Red Hat Singapore, specializing in automation and containerization with Ansible and OpenShift. 

He 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). He is the author of the book "Ansible for Real-Life Automation".

 

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

Browse by channel

automation icon

Automation

The latest on IT automation for tech, teams, and environments

AI icon

Artificial intelligence

Updates on the platforms that free customers to run AI workloads anywhere

open hybrid cloud icon

Open hybrid cloud

Explore how we build a more flexible future with hybrid cloud

security icon

Security

The latest on how we reduce risks across environments and technologies

edge icon

Edge computing

Updates on the platforms that simplify operations at the edge

Infrastructure icon

Infrastructure

The latest on the world’s leading enterprise Linux platform

application development icon

Applications

Inside our solutions to the toughest application challenges

Original series icon

Original shows

Entertaining stories from the makers and leaders in enterprise tech