proxmox_update_reboot.yml
· 3.9 KiB · YAML
Raw
---
- 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
1 | --- |
2 | - hosts: proxmox |
3 | strategy: linear |
4 | vars: |
5 | ansible_command_timeout: 600 # Increased timeout for potential long-running updates and reboots |
6 | ansible_ssh_extra_args: '-o StrictHostKeyChecking=no' |
7 | |
8 | pre_tasks: |
9 | - name: Check if reboot is required |
10 | stat: |
11 | path: /var/run/reboot-required |
12 | register: reboot_required_file |
13 | become: yes |
14 | |
15 | - name: Enter node maintenance mode if updates are pending |
16 | command: pvecm node-maintenance |
17 | become: yes |
18 | when: reboot_required_file.stat.exists |
19 | register: maintenance_mode_result |
20 | changed_when: maintenance_mode_result.rc == 0 |
21 | |
22 | - name: Verify SSH connectivity |
23 | ping: |
24 | register: ssh_connection_check |
25 | ignore_errors: yes |
26 | |
27 | - name: Fail if SSH connection is not working |
28 | fail: |
29 | msg: "Unable to establish SSH connection to the host" |
30 | when: ssh_connection_check.failed |
31 | |
32 | tasks: |
33 | - name: Update apt cache |
34 | apt: |
35 | update_cache: yes |
36 | become: yes |
37 | |
38 | - name: Upgrade all packages |
39 | apt: |
40 | upgrade: yes |
41 | autoremove: yes |
42 | autoclean: yes |
43 | become: yes |
44 | register: upgrade_result |
45 | |
46 | - name: Check if reboot is required after upgrades |
47 | stat: |
48 | path: /var/run/reboot-required |
49 | register: reboot_required |
50 | become: yes |
51 | |
52 | - hosts: 172.16.30.41 # First host |
53 | vars: |
54 | ansible_ssh_extra_args: '-o StrictHostKeyChecking=no' |
55 | |
56 | tasks: |
57 | - name: Reboot first host if required |
58 | reboot: |
59 | reboot_timeout: 300 # 5 minutes timeout for host to come back online |
60 | msg: "Rebooting for system updates" |
61 | become: yes |
62 | when: |
63 | - hostvars[inventory_hostname]['reboot_required'].stat.exists |
64 | - hostvars[inventory_hostname]['upgrade_result'].changed |
65 | |
66 | - name: Exit node maintenance mode |
67 | command: pvecm node-resume |
68 | become: yes |
69 | when: |
70 | - hostvars[inventory_hostname]['reboot_required'].stat.exists |
71 | - hostvars[inventory_hostname]['upgrade_result'].changed |
72 | register: maintenance_exit_result |
73 | changed_when: maintenance_exit_result.rc == 0 |
74 | |
75 | - name: Wait 30 seconds before next host |
76 | wait_for: |
77 | timeout: 30 |
78 | when: |
79 | - hostvars[inventory_hostname]['reboot_required'].stat.exists |
80 | - hostvars[inventory_hostname]['upgrade_result'].changed |
81 | |
82 | - hosts: 172.16.30.42 # Second host |
83 | vars: |
84 | ansible_ssh_extra_args: '-o StrictHostKeyChecking=no' |
85 | |
86 | tasks: |
87 | - name: Reboot second host if required |
88 | reboot: |
89 | reboot_timeout: 300 |
90 | msg: "Rebooting for system updates" |
91 | become: yes |
92 | when: |
93 | - hostvars[inventory_hostname]['reboot_required'].stat.exists |
94 | - hostvars[inventory_hostname]['upgrade_result'].changed |
95 | |
96 | - name: Exit node maintenance mode |
97 | command: pvecm node-resume |
98 | become: yes |
99 | when: |
100 | - hostvars[inventory_hostname]['reboot_required'].stat.exists |
101 | - hostvars[inventory_hostname]['upgrade_result'].changed |
102 | register: maintenance_exit_result |
103 | changed_when: maintenance_exit_result.rc == 0 |
104 | |
105 | - name: Wait 30 seconds before next host |
106 | wait_for: |
107 | timeout: 30 |
108 | when: |
109 | - hostvars[inventory_hostname]['reboot_required'].stat.exists |
110 | - hostvars[inventory_hostname]['upgrade_result'].changed |
111 | |
112 | - hosts: 172.16.30.43 # Third host |
113 | vars: |
114 | ansible_ssh_extra_args: '-o StrictHostKeyChecking=no' |
115 | |
116 | tasks: |
117 | - name: Reboot third host if required |
118 | reboot: |
119 | reboot_timeout: 300 |
120 | msg: "Rebooting for system updates" |
121 | become: yes |
122 | when: |
123 | - hostvars[inventory_hostname]['reboot_required'].stat.exists |
124 | - hostvars[inventory_hostname]['upgrade_result'].changed |
125 | |
126 | - name: Exit node maintenance mode |
127 | command: pvecm node-resume |
128 | become: yes |
129 | when: |
130 | - hostvars[inventory_hostname]['reboot_required'].stat.exists |
131 | - hostvars[inventory_hostname]['upgrade_result'].changed |
132 | register: maintenance_exit_result |
133 | changed_when: maintenance_exit_result.rc == 0 |