Ansible reboot Linux machine or server with playbooks

Ansible reboot Linux machine or server with playbooks

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


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


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.

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

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.




CentOS 7.0 Set Up OpenVPN Server In 5 Minutes

CentOS 7.0 Set Up OpenVPN Server In 5 Minutes

am a new CentOS Linux 7.0 server user. How do I set up an OpenVPN Server on CentOS Linux version 7.0 server to shield my browsing activity from bad guys on public Wi-Fi, and more?

Introduction OpenVPN is a full-featured SSL VPN (virtual private network). It implements OSI layer 2 or 3 secure network extension using the SSL/TLS protocol. It is an open source software and distributed under the GNU GPL. A VPN allows you to connect securely to an insecure public network such as wifi network at the airport or hotel. VPN is also required to access your corporate or enterprise or home server resources. You can bypass the geo-blocked site and increase your privacy or safety online. This tutorial provides step-by-step instructions for configuring an OpenVPN server on CentOS Linux 7.0 server.

Procedure: CentOS 7.0 Set Up OpenVPN Server In 5 Minutes

The steps are as follows:

Step 1 – Update your system

Run the yum command :
{vivek@centos7:~ }$ sudo yum update

Step 2 – Find and note down your IP address

Use the ip command as follows:
{vivek@centos7:~ }$ ip a 
{vivek@centos7:~ }$ ip a show eth0


Another option is to run the following dig command/host command to find out your public IP address from Linux command line:
{vivek@centos7:~ }$ dig +short
{vivek@centos7:~ }$ dig TXT +short | awk -F'"' '{ print $2}'



Finding out AWS/EC2 or Lighsail NAT IP address from the CLI

A note about IP address

Most CentOS Linux cloud servers have two types of IP address:

  1. Public static IP address directly assigned to your box and routed from the Internet. For example, Linode, Digital Ocean, and others gives you direct public IP address.
  2. Private static IP address directly attached to your server and your server is behind NAT with public IP address. For example, AWS EC2/Lightsail give you this kind of NAT public IP address.

The script will automatically detect your networking setup. All you have to do is provide correct IP address when asked for it.

Step 3 – Download and run script

I am going to use the wget command:
{vivek@centos7:~ }$ wget -O

Setup permissions using the chmod command
{vivek@centos7:~ }$ chmod +x
One can view the script using a text editor such as vim/vi :
{vivek@centos7:~ }$ vi


Now all you have to do is:
{vivek@centos7:~ }$ sudo ./
Sample session from AWS/Lightsail where my cloud server is behind NAT:

Sample session from Linode/DO server where cloud server has direct public IPv4 address:

To avoid problem always choose DNS as or Google DNS. Those are fast DNS server and reached from anywhere on the Internet.


{vivek@centos7:~ }$ sudo systemctl stop openvpn@server # <--- stop server
{vivek@centos7:~ }$ sudo systemctl start openvpn@server # <--- start server
{vivek@centos7:~ }$ sudo systemctl restart openvpn@server # <--- restart server
{vivek@centos7:~ }$ sudo systemctl status openvpn@server # <--- get server status

Step 4 – Connect an OpenVPN server using IOS/Android/Linux/Windows client

On server your will find a client configuration file called ~/desktop.ovpn. All you have to do is copy this file to your local desktop using the scp command:
{vivek@centos7:~ }$ scp [email protected]:~/deskcop.ovpn .
Next, provide this file to your OpenVPN client to connect:

  1. Apple iOS client
  2. Android client
  3. Apple MacOS (OS X) client
  4. Windows 8/10 client


First, install the openvpn client for your desktop, enter:
{vivek@centos7:~ }$ sudo yum install openvpn
{vivek@centos7:~ }$ sudo apt install openvpn
Next, copy desktop.ovpn as follows:
{vivek@centos7:~ }$ sudo cp desktop.ovpn /etc/openvpn/client.conf
Test connectivity from the CLI:
{vivek@centos7:~ }$ sudo openvpn --client --config /etc/openvpn/desktop.conf
Your Linux desktop system will automatically connect when computer restart using openvpn script/service:
{vivek@centos7:~ }$ sudo systemctl enable openvpn@client 
{vivek@centos7:~ }$ sudo systemctl start openvpn@client

Step 5 – Verify/test the connectivity

Execute the following commands after connecting to OpenVPN server from your Linux desktop:
{vivek@centos7:~ }$ ping #Ping to the OpenVPN server gateway
{vivek@centos7:~ }$ ip route #Make sure routing setup working
{vivek@centos7:~ }$ dig TXT +short #Must return public IP address of OpenVPN server

A note about trouble shooting OpenVPN server and client issues

Check OpenVPN server for errors:
{vivek@centos7:~ }$ journalctl --identifier openvpn


Is firewall rule setup correctly on your server? Use the cat command to see rules:
{vivek@centos7:~ }$ cat /etc/iptables/

iptables -t nat -A POSTROUTING -s -o eth0 -j MASQUERADE
iptables -A INPUT -i tun0 -j ACCEPT
iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT
iptables -A FORWARD -i tun0 -o eth0 -j ACCEPT
iptables -A INPUT -i eth0 -p udp --dport 1194 -j ACCEPT

Another option is to run iptables command and sysctl command commands to verify NAT rule setup on your server:
{vivek@centos7:~ }$ sudo iptables -t nat -L -n -v 
{vivek@centos7:~ }$ sysctl net.ipv4.ip_forward

Insert the rules if not inserted from /etc/iptables/
{vivek@centos7:~ }$ sudo sh /etc/iptables/ 
{vivek@centos7:~ }$ sudo sysctl -w net.ipv4.ip_forward=1

Is OpenVPN server running and port is open? Use the ss command or netstat command and pidof command/ps command:
{vivek@centos7:~ }$ netstat -tulpn | grep :1194 ## 1194 is the openvpn server port ##
{vivek@centos7:~ }$ ss -tulpn | grep :1194 ## 1194 is the openvpn server port ##
{vivek@centos7:~ }$ ps aux | grep openvpn ## is the openvpn server running? ##
{vivek@centos7:~ }$ ps -C openvpn ## is the openvpn server running? ##
{vivek@centos7:~ }$ pidof openvpn ## find the openvpn server PID ##

If not running, restart the OpenVPN server:
{vivek@centos7:~ }$ sudo systemctl restart openvpn@server
Look out for errors:
{vivek@centos7:~ }$ sudo systemctl status openvpn@server
Can the Linux desktop client connect to the OpenVPN server machine? First you need to run a simple test to see if the OpenVPN server port (UDP 1194) accepts connections:
{vivek@centos7:~ }$ nc -vu 1194 
Connection to 1194 port [udp/openvpn] succeeded!

If not connected it means either a Linux desktop firewall or your router is blocking access to server. Make sure both client and server using same protocol and port, e.g. UDP port 1194.


Congratulations. You successfully set up an OpenVPN server on CentOS Linux 7.0 server running in the cloud. See the OpenVPN website here and script site here for