Ansible / Automation / DevOps / How To
A reboot operation in a Linux Operating System is very rare as most of the time we can achieve the result by individual service restart or reload after configuration changes or package update. But in some cases like patching or system upgrade, a reboot operation is required to complete the full workflow and effect. If you are using Ansible for your operation, you need to automate this reboot task as well to accomplish full automation without a manual reboot task.
Also Read : Learn Ansible Automation
This is very easy in Ansible as you have multiple methods to achieve a system reboot task.
Make sure, the remote_user
– which you are using for Ansible to login to the managed node – should have sudo
access and the permission to reboot the node.
Earlier we used to reboot a managed node using Ansible by a 2 step method. (There are so many ways, but just explaining a common method). First, we will trigger a reboot command using the shell
module and in the second task, we will use a wait_for_connection
module to check the node coming back online after reboot.
Something like below.
- 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 simple and efficient method with the Ansible reboot
module and you can achieve the task with a single task.
reboot
moduleAnsible reboot
module was introduced in Ansible 2.7 (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.
- name: Reboot the machine with all defaults options
reboot:
There are options like reboot_timeout
for waiting timeout or pre_reboot_delay
which will wait until mentioned seconds before reboot etc. Refer to the Ansible Reboot Module documentation for details.
---
- 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
remote_user
is configured with a sudo
password, make sure you add -K
argument and enter the sudo
password.$ 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
sudo
password in credential as shown below.What about Windows Machine rebooting ? As you know you can manage Windows machines as well using Ansible and this reboot task will be needed as part of your automation workflow.
Learn how to configure Windows Node for Managing by Ansible.
And the good news is that, like any other windows alternative there is a dedicated Ansible win_reboot module to handle this.
- name: Reboot a Windows Machine
ansible.windows.win_reboot:
reboot_timeout: 360
Refer to the Ansible win_reboot module Documentation for details.
Disclaimer:
The views expressed and the content shared in all published articles on this website are solely those of the respective authors, and they do not necessarily reflect the views of the author’s employer or the techbeatly platform. We strive to ensure the accuracy and validity of the content published on our website. However, we cannot guarantee the absolute correctness or completeness of the information provided. It is the responsibility of the readers and users of this website to verify the accuracy and appropriateness of any information or opinions expressed within the articles. If you come across any content that you believe to be incorrect or invalid, please contact us immediately so that we can address the issue promptly.
Gineesh Madapparambath
Gineesh Madapparambath is the founder of techbeatly and he is the co-author of The Kubernetes Bible, Second Edition. and the author of 𝗔𝗻𝘀𝗶𝗯𝗹𝗲 𝗳𝗼𝗿 𝗥𝗲𝗮𝗹-𝗟𝗶𝗳𝗲 𝗔𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻.
He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform).
(aka Gini Gangadharan - iamgini.com)
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Leave a Reply