• 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
  • Web Tools
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
  • Web Tools
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
66
VIEWS
Share on FacebookShare on LinkedIn

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:

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

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

ADVERTISEMENT
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:

  • 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

Next Post

Host a Website on Raspberry Pi – Real Benchmark Tests

neo

neo

Related Posts

10+ Useful Linux find Command Examples You Should Know
Commands

10+ Useful Linux find Command Examples You Should Know

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 Installed Linux Packages

Automating Linux Server Health Checks with a Bash Script
Linux Tutorials

Automating Linux Server Health Checks with a Bash Script

Next Post
Host a Website on Raspberry Pi – Real Benchmark Tests

Host a Website on Raspberry Pi – Real Benchmark Tests

Leave a Reply

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


Recommended

Lesson 5: Linux Bash Read User Input with Example

Lesson 5: Linux Bash Read User Input with Example

MikroTik: Configure SNMP for Network Monitoring

MikroTik: Configure SNMP for Network Monitoring

Install Portainer on Ubuntu 24.04 Using Docker Compose

Install Portainer on Ubuntu 24.04 Using Docker Compose

FortiGate Country Address Object Generator – Block or Allow Access by Country

FortiGate Country Address Object Generator – Block or Allow Access by Country

View & Copy Image Metadata Online – Camera & GPS Info

View & Copy Image Metadata Online – Camera & GPS Info

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
  • Web Tools
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

Install Portainer on Ubuntu 24.04 Using Docker Compose

Install Portainer on Ubuntu 24.04 Using Docker Compose

FortiGate Country Address Object Generator – Block or Allow Access by Country

FortiGate Country Address Object Generator – Block or Allow Access by Country

  • 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
  • Web Tools

© 2025 defencedev.com - All rights reserved.