Ubuntu: reboot only servers that need it, one at a time

Rewrite reboot.yaml to reboot ubuntu_servers flagged with
/run/reboot-required: pi-hole first, the rest one at a time, and the
Ansible controller last, with a ntfy alert per server. Parallel boots
overloaded the Proxmox host and services timed out at boot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
JustinandClaude Opus 5.5 committed 2026-11-19 19:57:06 -06:00
1 parent 84433d5cbc
commit fb54627f38
1 file changed
+80 -9
+80 -9
View File
@@ -1,17 +1,88 @@
--- ---
- name: Rebooting Machines Proxmox # Reboot Ubuntu servers that have /run/reboot-required set, with a ntfy alert per server.
hosts: proxmox # Order: pi-hole (DNS) first and alone, then the rest a few at a time, then this
become: true # Ansible controller last, so it never reboots out from under the run.
# The hostnames for the DNS server and the controller are also in the play patterns below.
tasks: - name: Find servers that need a reboot
- name: Checking Reboot is needed hosts: ubuntu_servers
become: true become: true
gather_facts: false
tasks:
- name: Check for the reboot-required flag
ansible.builtin.stat: ansible.builtin.stat:
path: /run/reboot-required path: /run/reboot-required
register: reboot_required register: reboot_required
- name: Reboot Machine - name: Group servers that need a reboot
become: true ansible.builtin.group_by:
ansible.builtin.reboot: key: needs_reboot
reboot_timeout: 3600
when: reboot_required.stat.exists when: reboot_required.stat.exists
- name: Reboot servers
hosts: needs_reboot:!ansible.ki5bhv.com
become: true
gather_facts: false
vars:
dns_server: pi-hole-server.ki5bhv.com
ntfy_url: https://ntfy.ki5bhv.com/server
tasks:
- name: Reboot and notify
block:
# Runs on its own before anything else, so the other servers boot with DNS available.
- name: Reboot DNS server first
ansible.builtin.reboot:
reboot_timeout: 1800
when: inventory_hostname == dns_server
- name: Reboot remaining servers
ansible.builtin.reboot:
reboot_timeout: 1800
when: inventory_hostname != dns_server
throttle: 1 # one at a time: parallel boots overload the Proxmox host and services time out
- name: Notify reboot succeeded
ansible.builtin.uri:
url: "{{ ntfy_url }}"
method: POST
body: "{{ inventory_hostname }} needed a reboot and was rebooted"
delegate_to: localhost
become: false
rescue:
- name: Notify reboot failed
ansible.builtin.uri:
url: "{{ ntfy_url }}"
method: POST
body: "{{ inventory_hostname }} needed a reboot but it FAILED - check reboot.log"
headers:
Priority: high
delegate_to: localhost
become: false
- name: Mark host as failed
ansible.builtin.fail:
msg: "Reboot of {{ inventory_hostname }} failed"
- name: Reboot this Ansible controller last
hosts: needs_reboot:&ansible.ki5bhv.com
become: true
gather_facts: false
vars:
ntfy_url: https://ntfy.ki5bhv.com/server
tasks:
# Sent first: once the reboot happens, nothing is left to report back.
- name: Notify controller reboot
ansible.builtin.uri:
url: "{{ ntfy_url }}"
method: POST
body: "{{ inventory_hostname }} (Ansible controller) needed a reboot and is rebooting in 1 minute"
delegate_to: localhost
become: false
# The reboot module can't wait for the machine it is running on, so schedule it
# a minute out and let this run finish and write its log first.
- name: Schedule reboot in 1 minute
ansible.builtin.command:
argv: [shutdown, -r, "+1", "Ansible - reboot required"]
changed_when: true