--- - hosts: proxmox strategy: linear vars: ansible_command_timeout: 600 # Increased timeout for potential long-running updates and reboots ansible_ssh_extra_args: '-o StrictHostKeyChecking=no' pre_tasks: - name: Check if reboot is required stat: path: /var/run/reboot-required register: reboot_required_file become: yes - name: Enter node maintenance mode if updates are pending command: pvecm node-maintenance become: yes when: reboot_required_file.stat.exists register: maintenance_mode_result changed_when: maintenance_mode_result.rc == 0 - name: Verify SSH connectivity ping: register: ssh_connection_check ignore_errors: yes - name: Fail if SSH connection is not working fail: msg: "Unable to establish SSH connection to the host" when: ssh_connection_check.failed tasks: - name: Update apt cache apt: update_cache: yes become: yes - name: Upgrade all packages apt: upgrade: yes autoremove: yes autoclean: yes become: yes register: upgrade_result - name: Check if reboot is required after upgrades stat: path: /var/run/reboot-required register: reboot_required become: yes - hosts: 172.16.30.41 # First host vars: ansible_ssh_extra_args: '-o StrictHostKeyChecking=no' tasks: - name: Reboot first host if required reboot: reboot_timeout: 300 # 5 minutes timeout for host to come back online msg: "Rebooting for system updates" become: yes when: - hostvars[inventory_hostname]['reboot_required'].stat.exists - hostvars[inventory_hostname]['upgrade_result'].changed - name: Exit node maintenance mode command: pvecm node-resume become: yes when: - hostvars[inventory_hostname]['reboot_required'].stat.exists - hostvars[inventory_hostname]['upgrade_result'].changed register: maintenance_exit_result changed_when: maintenance_exit_result.rc == 0 - name: Wait 30 seconds before next host wait_for: timeout: 30 when: - hostvars[inventory_hostname]['reboot_required'].stat.exists - hostvars[inventory_hostname]['upgrade_result'].changed - hosts: 172.16.30.42 # Second host vars: ansible_ssh_extra_args: '-o StrictHostKeyChecking=no' tasks: - name: Reboot second host if required reboot: reboot_timeout: 300 msg: "Rebooting for system updates" become: yes when: - hostvars[inventory_hostname]['reboot_required'].stat.exists - hostvars[inventory_hostname]['upgrade_result'].changed - name: Exit node maintenance mode command: pvecm node-resume become: yes when: - hostvars[inventory_hostname]['reboot_required'].stat.exists - hostvars[inventory_hostname]['upgrade_result'].changed register: maintenance_exit_result changed_when: maintenance_exit_result.rc == 0 - name: Wait 30 seconds before next host wait_for: timeout: 30 when: - hostvars[inventory_hostname]['reboot_required'].stat.exists - hostvars[inventory_hostname]['upgrade_result'].changed - hosts: 172.16.30.43 # Third host vars: ansible_ssh_extra_args: '-o StrictHostKeyChecking=no' tasks: - name: Reboot third host if required reboot: reboot_timeout: 300 msg: "Rebooting for system updates" become: yes when: - hostvars[inventory_hostname]['reboot_required'].stat.exists - hostvars[inventory_hostname]['upgrade_result'].changed - name: Exit node maintenance mode command: pvecm node-resume become: yes when: - hostvars[inventory_hostname]['reboot_required'].stat.exists - hostvars[inventory_hostname]['upgrade_result'].changed register: maintenance_exit_result changed_when: maintenance_exit_result.rc == 0