Ansible reboot Linux machine or server with playbooks nixCraft

I need to reboot the VM or bare metal Linux machine/server using Ansible and wait for it to come back, but it does not work with playbook as descried here. How to reboot Linux server with Ansible? How do I reboot and wait for reboot to complete in Ansible playbook for multiple Linux servers?

Introduction: You can reboot a Linux or Unix based machine, wait for it to go down (say for kernel update), come back up, and respond to commands. You can use either command or shell module to reboot the Linux server when kernel updated. However, now we have a reboot module to reboot a machine using Ansible. I tested this module with:

  1. Ubuntu Linux 16.04 / 18.04 LTS
  2. CentOS Linux 7
  3. Debian Linux 9.x
  4. RHEL 7.x
  5. SUSE 12.x
  6. FreeBSD
  7. OpenBSD


Please note that you must have Ansible version 2.7 to work with the reboot module:
$ ansible --version
Linux find out ansible version command
If not using Ansible version 2.7, try to update it using the dnf command/yum command/apt command/apt-get command as per your Linux distro version:
$ sudo apt update ## Debian or Ubuntu box ##
$ sudo yum update ## RHEL/CentOS 7 ##

Ansible reboot Linux machine or server with playbooks

The syntax is pretty simple to do reboot:

- name: Reboot the machine with all defaults using Ansible reboot:

Here is a sample hosts file displayed using the cat command:

ansible_user='{{ my_c_user }}'
ansible_become_pass='{{ my_c_sudo_pass }}' [legacy]
do-de.public [cluster]
ln.cbz01 ln.cbz02 ln.cbz04 [lxd]
do-de.public [vpn:vars]
ansible_python_interpreter='/usr/bin/env python3' [vpn]
do-blr-vpn [backup]
gcvm.backup [nodes:children]
legacy [isrestart:children]

Here is my reboot.yml:

- hosts: isrestart become: true become_user: root tasks: - name: Rebooting the cloud server/bare metal box reboot:

How to use Ansible reboot module playbook to reboot the box

Now all you have to do is run playbook (see how to set and use sudo password for Ansible Vault)
$ ansible-playbook -i hosts --ask-vault-pass --extra-vars '' reboot.yml
Ansible reboot Linux machine or server with playbooks commands

How to reboot a machine and set time out value

By default Ansible reboot module waits 600 seconds. You can increase value using the following syntax:

- name: Reboot a Linux machine reboot: reboot_timeout: 1800

How to set command to run on the rebooted host and expect success from to determine the machine is ready for further tasks

By default whoami command used by ansbile. You can change it as follows:

- name: Reboot a Linux machine reboot: test_command: uptime


- name: Reboot a Linux machine reboot: test_command: ping -c 4

How to set pre and post reboot delay

One can force Ansible to wait after the reboot was successful and the connection was re-established in seconds as follows:

- name: Unconditionally reboot the machine with all defaults reboot: post_reboot_delay: 180

The above is useful if you want wait for additional networking/storage or server vpn to kick in despite your connection already working. You can also set time for shutdown to wait before requesting reboot:

- name: Unconditionally reboot the machine with all defaults reboot: pre_reboot_delay: 180


You just learned how to reboot Linux/Unix box and wait for reboot to complete in Ansible playbook. For more info see this page here.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Facebook Comments

More Stuff

How to align your team around microservices Microservices have been a focus across the open source world for several years now. Although open source technologies such as Docker, Kubernetes, Prom...
10 Useful “IP” Commands to Configure Network Interfaces In this post, we are going to review how we can assign Static IP Address, Static Route, Default Gateway etc.  Assigning IP Address on demand using IP ...
How to Install and Switch Desktop Environments in Fedora Do you want to use or try a different desktop environment in Fedora Workstation spin, other than the default, GNOME 3. In this article, we will show h...
Testing in production: Yes, you can (and should) I wrote a piece recently about why we are all distributed systems engineers now. To my surprise, lots of people objected to the observation that you h...
Spread the love

Posted by News Monkey