Simple Steps to Reboot Servers Using Ansible

Prazwal
by Prazwal 

Ansible is an open-source automation tool that simplifies configuration management, application deployment and other tasks across various servers and systems. The scripting language used by Ansible is in YAML format making it easier to learn and manage compared to complex scripting languages. The purpose of this is to reboot multiple servers using ansible and wait until the reboot is completed & verification is done.

For instance , imagine managing hundreds of servers, keeping them up-to-date and functioning optimally requires regular reboots. But manually rebooting each server can be monotonous and error-prone as well. This is where this ansible-playbook script fits. 

Normally, if we just reboot the server then the play will fail giving error “Shared connection failed”

But this can be skipped in this particular way by rebooting the server and waiting till the reboot is completed and checking post reboot validations as well.

Prerequisites

You must have ansible version 2.7 or above to work with this particular module. To check this run below command:

$ ansible --version

command to check ansible version

Writing the Playbook

Write a yml script for reboot. Here I have named reboot file reboot.yml . For this I have created yml file

vim reboot.yml

I have used wait_for_connection module to check the after reboot validations and wait for the reboot completion during the time of reboot.

---
- name: Reboot the server
  hosts: all  # This targets all hosts in your inventory 
  become: true
  tasks:

      - name: Reboot the remote server
        tags: reboot
        become: yes
        become_user: root
        shell: "sleep 5 && reboot"
        async: 1
        poll: 0

      - name: Waiting for reboot to complete
        wait_for_connection:
          connect_timeout: 10
          sleep: 5
          delay: 5
          timeout: 300

      - name: Printing the uptime of servers
        shell: "uptime"
        register: Uptime

      - debug: var=Uptime

Executing the Playbook & Verifying the Reboot

The above script will give the following output after execution.

$ ansible-playbook reboot.yml -i inventory 

PLAY [Reboot the server] ***********************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [minion]
ok: [master]

TASK [Reboot the remote server] ****************************************************************************************************************************************************************************
changed: [master]
changed: [minion]

TASK [Waiting for reboot to complete] **********************************************************************************************************************************************************************
ok: [master]
ok: [minion]

TASK [Printing the uptime of servers] **********************************************************************************************************************************************************************
changed: [minion]
changed: [master]

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [minion] => {
    "Uptime": {
        "changed": true,
        "cmd": "uptime",
        "delta": "0:00:00.004397",
        "end": "2024-05-23 08:35:16.359563",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2024-05-23 08:35:16.355166",
        "stderr": "",
        "stderr_lines": [],
        "stdout": " 08:35:16 up 4 min,  1 user,  load average: 0.07, 0.04, 0.00",
        "stdout_lines": [
            " 08:35:16 up 4 min,  1 user,  load average: 0.07, 0.04, 0.00"
        ]
    }
}
ok: [master] => {
    "Uptime": {
        "changed": true,
        "cmd": "uptime",
        "delta": "0:00:00.004345",
        "end": "2024-05-23 08:35:16.441761",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2024-05-23 08:35:16.437416",
        "stderr": "",
        "stderr_lines": [],
        "stdout": " 08:35:16 up 4 min,  1 user,  load average: 0.01, 0.06, 0.03",
        "stdout_lines": [
            " 08:35:16 up 4 min,  1 user,  load average: 0.01, 0.06, 0.03"
        ]
    }
}

PLAY RECAP *************************************************************************************************************************************************************************************************
master                     : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
minion                     : ok=5    changed=2    unreac

Conclusion

The above playbook can be used to reboot servers using ansible and wait till reboot is completed and check post reboot verification as well. It can be multipurpose and used for many purposes dynamically.

Explanation of the Script

If you have reached here then you might be wondering what are those terms which executed the reboot process. I would like to describe it here:

connect_timeout: This is the maximum number of seconds to wait for a connection to happen before closing or retrying again.

sleep: It explains how many seconds to sleep in between checks.

delay: It explains how many seconds to wait before starting to poll.

timeout: Maximum number of seconds to wait for before timeout

Thank you for reading through the end! If you found the blog helpful, share it to someone who is a nerd like you. Keep reading, learning, and growing!

.…………….

Vagrant is an open-source tool designed to help developers create and manage virtualized development environments in a simple and reproducible manner. Read about virtualization with vagrant in this article.

Gurzu is a software development company passionate about building software that solve real-life problems. Explore some of our awesome projects in our success stories.

Have a tech idea that you need help turning into reality? Book a free consulting session with us!