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