Ansible reboot Linux machine or server with playbooks

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?

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.
Tutorial requirements
RequirementsLinux/macOS/Unix-like system with Ansible v2.7+
Root privileges No
Difficulty Easy
Est. reading time 5 mintues
I tested this module with:
  1. Ubuntu Linux 16.04 / 18.04 / 20.04 LTS
  2. CentOS Linux 7/8
  3. Debian Linux 9.x/10.x/11.x
  4. RHEL 7.x/8.x
  5. OpenSUSE and SUSE 12.x/15.x
  6. FreeBSD
  7. OpenBSD


Please note that you must have Ansible version 2.7 or above to work with the reboot module:
$ ansible --version

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

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

ansible_user='{{ my_c_user }}'
ansible_become_pass='{{ my_c_sudo_pass }}'
ansible_python_interpreter='/usr/bin/env python3'

Here is my reboot.yml:

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

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

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_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 
    test_command: uptime


- name: Reboot a Linux machine 
    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
    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
    pre_reboot_delay: 180

View reboot log history on the Linux server

Let us say I am doing a conditional reboot of my Ubuntu or Debian Linux box. For instance, my Ansible playbook:

      - name: Check if a reboot is needed on AWS EC2 Ubuntu/Debian based servers
        register: reboot_required_file
        stat: path=/var/run/reboot-required get_md5=no
      - name: Reboot the box if kernel updated/installed on EC2 
          msg: "Reboot initiated by Ansible for kernel updates"
          connect_timeout: 5
          reboot_timeout: 300
          pre_reboot_delay: 0
          post_reboot_delay: 30
          test_command: uptime
        when: reboot_required_file.stat.exists

We can search for “Reboot initiated by Ansible for kernel updates” on our server to see when my box was rebooted using the grep command/zgrep command:
$ ssh
$ sudo grep 'reboot' /var/log/auth.log
$ sudo zgrep 'Reboot initiated by Ansible for kernel updates' /var/log/auth.log*
$ sudo zgrep 'reboot' /var/log/auth.log*

Sample outputs:

auth.log:Jun  9 11:06:57 ls-debian-10 systemd-logind[488]: System is rebooting (Reboot initiated by Ansible for kernel updates).
auth.log.2.gz:May 27 04:55:54 ip-172-26-14-129 sudo:    admin : TTY=pts/0 ; PWD=/home/admin ; USER=root ; COMMAND=/sbin/reboot

Another option is to run the last command:
$ sudo last -x "reboot"
Reboot history:

reboot   system boot  4.19.0-9-amd64   Wed Jun 10 03:51   still running
reboot   system boot  4.19.0-9-amd64   Tue Jun  9 11:07 - 03:51  (16:43)
reboot   system boot  4.19.0-9-amd64   Wed May 27 04:56 - 11:06 (13+06:10)
reboot   system boot  4.9.0-12-amd64   Wed May 27 04:15 - 04:55  (00:40)
reboot   system boot  4.9.0-8-amd64    Wed May 27 04:09 - 04:14  (00:05)
reboot   system boot  4.9.0-8-amd64    Wed May 27 04:08 - 04:09  (00:01)

wtmp begins Wed May 27 04:08:01 2020

See “How To Find Out Last System Linux Reboot Time and Date Command” for more information


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

This entry is 2 of 2 in the Ansible Reboot a Machine/Server Tutorial series. Keep reading the rest of the series:
  1. Ansible reboot a Debian/Ubuntu Linux for kernel update and wait for it
  2. Ansible reboot Linux machine or server with playbooks

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Pankaj Apr 18, 2021 @ 9:21

    Great article!

    Tough the Link to Ansible Docs, in Conclusion, is 404; can you please link the good one?

    • 🐧 Vivek Gite Apr 18, 2021 @ 11:39

      Look like they removed or changed documentations.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum