--- # Reboot hosts with rolling strategy to avoid taking down redundant services # # Usage examples: # # Reboot a single host # ansible-playbook reboot.yml -l testvm01 # # # Reboot all test hosts (one at a time) # ansible-playbook reboot.yml -l tier_test # # # Reboot all DNS servers safely (one at a time) # ansible-playbook reboot.yml -l role_dns # # Safety features: # - serial: 1 ensures only one host reboots at a time # - Waits for host to come back online before proceeding # - Groups hosts by role to avoid rebooting same-role hosts consecutively - name: Reboot hosts (rolling) hosts: all serial: 1 order: shuffle # Randomize to spread out same-role hosts gather_facts: false vars: reboot_timeout: 300 # 5 minutes to wait for host to come back tasks: - name: Display reboot target ansible.builtin.debug: msg: "Rebooting {{ inventory_hostname }} (role: {{ role | default('none') }})" - name: Reboot the host ansible.builtin.systemd: name: reboot.target state: started async: 1 poll: 0 ignore_errors: true - name: Wait for host to come back online ansible.builtin.wait_for_connection: delay: 5 timeout: "{{ reboot_timeout }}" - name: Display reboot result ansible.builtin.debug: msg: "{{ inventory_hostname }} rebooted successfully"