Skip to content

Commit bdaab4c

Browse files
committed
fix(ansible): update reboot playbook
1 parent c416218 commit bdaab4c

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

ansible/play-any--reboot.yml

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,65 @@
1515
path: /var/run/reboot-required
1616
get_checksum: false
1717

18-
- name: Print uptime
19-
debug:
20-
msg:
21-
'Host machine {{ inventory_hostname }} has been up for {{
22-
ansible_facts.uptime_seconds/86400 }} days'
23-
2418
# Reboot for the following reasons:
2519
# 1. /var/run/reboot-required exists (e.g. kernel update)
2620
# 2. Uptime is greater than 7 days
2721
# 3. force_reboot is set to True
28-
- name: Debug
22+
- name: Display Status
2923
debug:
3024
msg:
31-
'Debug info: Reboot Required File Exists: {{
32-
reboot_required.stat.exists }}, Uptime: {{
33-
ansible_facts.uptime_seconds/86400 }} days, Force Reboot Flag: {{
34-
force_reboot }}'
25+
- 'Debug info:'
26+
- ' Force Reboot Flag: {{ force_reboot }}'
27+
- ' Reboot Required File Exists: {{ reboot_required.stat.exists }}'
28+
- ' Uptime: {{ ansible_facts.uptime_seconds/86400 }} days'
3529

3630
- name: Set Should Reboot
3731
set_fact:
38-
should_reboot:
39-
'{{ reboot_required.stat.exists or ansible_facts.uptime_seconds >
40-
604800 or force_reboot }}'
32+
should_reboot: >-
33+
{% if force_reboot %} true {% elif reboot_required.stat.exists %} true
34+
{% elif ansible_facts.uptime_seconds > 604800 %} true {% else %} false
35+
{% endif %}
36+
reboot_reason: >-
37+
{%- set reasons = [] -%} {%- if force_reboot -%}
38+
{%- set reasons = reasons + ['manual force reboot requested'] -%}
39+
{%- endif -%} {%- if reboot_required.stat.exists -%}
40+
{%- set reasons = reasons + ['kernel update or package requires
41+
reboot'] -%} {%- endif -%} {%- if ansible_facts.uptime_seconds >
42+
604800 -%}
43+
{%- set reasons = reasons + ['system uptime exceeds 7 days'] -%}
44+
{%- endif -%} {%- if reasons|length == 0 -%}
45+
No reboot needed
46+
{%- else -%}
47+
{{ reasons|join(', ') }}
48+
{%- endif -%}
4149
42-
- name: Debug
50+
- name: Print Reboot Reason
4351
debug:
44-
msg: 'Rebooting the machine: {{ inventory_hostname }}'
52+
msg:
53+
'Rebooting the machine: {{ inventory_hostname }} - Reason: {{
54+
reboot_reason }}'
4555
when: should_reboot
4656

47-
- name: Conditionally reboot the machine
48-
ansible.builtin.reboot:
49-
connect_timeout: 5
50-
reboot_timeout: 300
51-
pre_reboot_delay: 15
52-
post_reboot_delay: 15
53-
test_command: uptime
57+
- name: Conditionally Reboot
58+
block:
59+
- name: Reboot machine
60+
ansible.builtin.reboot:
61+
connect_timeout: 60
62+
reboot_timeout: 600
63+
pre_reboot_delay: 5
64+
post_reboot_delay: 30
65+
test_command: uptime
66+
msg: 'Reboot initiated by Ansible'
67+
register: reboot_result
68+
ignore_unreachable: true
69+
70+
- name: Wait for machine to become reachable again
71+
wait_for_connection:
72+
delay: 10
73+
timeout: 300
74+
75+
- name: Gather facts after reboot
76+
setup:
77+
5478
when: should_reboot
79+
ignore_errors: '{{ ansible_check_mode }}'

0 commit comments

Comments
 (0)