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.

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:
TASK [Reboot the system if needed] *******************************************************************
skipping: [pi4]

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.