• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Linux Tutorials

Automating Linux Server Reboot with Ansible

neo by neo
September 3, 2025
in Linux Tutorials
0
Automating Linux Server Reboot with Ansible

Automating Linux Server Reboot with Ansible

0
SHARES
16
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Linux Server Reboot with Ansible

Maintaining Linux servers often requires reboots after updates or configuration changes. Manually checking each server can be time-consuming. In this guide, I’ll show you how to use Ansible to automatically check if a server needs a reboot and perform it if necessary.

This keeps my systems clean and saves me from logging into multiple servers.

My Use Case with Linux/Raspberry Pi

I manage multiple Raspberry Pi servers in my home lab, and I wanted a way to automatically reboot them whenever required. Instead of logging into each server manually, I created an Ansible playbook that handles this task.

Server Reboot with Ansible
Server Reboot with Ansible

Hosts Definition

I defined my servers in a hosts.ini file under the Ansible inventory directory:

[pi_servers]
pi4 ansible_host=192.168.20.110 ansible_user=my_user_on_server

Here, [pi_servers] is the group name, pi4 is the host alias, and ansible_user specifies the SSH user for connection.

Ansible Playbook Example

The playbook reboot-if-needed.yml checks if the server requires a reboot and executes it if needed:

 nano /opt/ansible/playbooks/reboot-if-needed.yml
---
- name: Reboot servers if needed
  hosts: pi_servers
  become: yes
  tasks:
    - name: Check if reboot is required
      ansible.builtin.stat:
        path: /var/run/reboot-required
      register: reboot_required

    - name: Reboot the system if needed
      ansible.builtin.reboot:
        reboot_timeout: 300
      when: reboot_required.stat.exists

Running the Playbook

You can execute the playbook with the following command:

cd /opt/ansible/playbooks/
ansible-playbook -i ../inventory/hosts.ini reboot-if-needed.yml --ask-become-pass

Here, --ask-become-pass will prompt for the sudo password if required.

Expected Result

If the server requires a reboot, you’ll see output similar to:

ADVERTISEMENT
TASK [Reboot the system if needed] *******************************************************************
changed: [pi4]

If no reboot is required, the task will be skipped:

ADVERTISEMENT
TASK [Reboot the system if needed] *******************************************************************
skipping: [pi4]
Ansible Server Reboot Expected Reboot
Ansible Server Reboot Expected Reboot

What Could Be Automated Next?

This use case is very simple, but it shows the power of Ansible. Other useful automations could be:

ADVERTISEMENT
  • automatic package updates
  • service restarts if they fail
  • disk space monitoring
  • regular backups

Conclusion

A small script plus Ansible can automate server maintenance in a reliable way. Even a basic task like “reboot if needed” becomes effortless when applied to multiple servers.

The real value comes when you keep extending your playbooks with more automation for everyday operations.

About The Author

neo

See author's posts

Tags: ansible
ADVERTISEMENT
Previous Post

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

neo

neo

Related Posts

GitHub: How to Add a Script or a Folder to Your Repository
Linux Tutorials

GitHub: How to Add a Script or a Folder to Your Repository

My Ultimate Linux/Ubuntu Commands Cheat Sheet (2025)
Linux Tutorials

My Ultimate Linux/Ubuntu Commands Cheat Sheet (2025)

Linux Networking: Display Open Ports and Listening Services
Networking

Linux Networking: Display Open Ports and Listening Services

List Installed Packages on Linux
Linux Tutorials

List of Installed Packages on Linux

Automating Linux Server Health Checks with a Bash Script
Linux Tutorials

Automating Linux Server Health Checks with a Bash Script

Check Disk Usage on the Linux with du Command
Commands

Check Disk Usage on the Linux with du Command

Leave a Reply

Your email address will not be published. Required fields are marked *


Recommended

MikroTik: Reset to Factory Settings with Terminal CLI

MikroTik: Reset to Factory Settings with Terminal CLI

netstat example on Ubuntu

Linux Networking: netstat (Network Statistics) Examples on Linux

Automating Linux Server Reboot with Ansible

Automating Linux Server Reboot with Ansible

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik: OpenVPN Client Failover Script

MikroTik: OpenVPN Client Failover Script

Categories

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos
ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Recent News

Automating Linux Server Reboot with Ansible

Automating Linux Server Reboot with Ansible

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.