|
15 | 15 | path: /var/run/reboot-required |
16 | 16 | get_checksum: false |
17 | 17 |
|
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 | | - |
24 | 18 | # Reboot for the following reasons: |
25 | 19 | # 1. /var/run/reboot-required exists (e.g. kernel update) |
26 | 20 | # 2. Uptime is greater than 7 days |
27 | 21 | # 3. force_reboot is set to True |
28 | | - - name: Debug |
| 22 | + - name: Display Status |
29 | 23 | debug: |
30 | 24 | 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' |
35 | 29 |
|
36 | 30 | - name: Set Should Reboot |
37 | 31 | 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 -%} |
41 | 49 |
|
42 | | - - name: Debug |
| 50 | + - name: Print Reboot Reason |
43 | 51 | debug: |
44 | | - msg: 'Rebooting the machine: {{ inventory_hostname }}' |
| 52 | + msg: |
| 53 | + 'Rebooting the machine: {{ inventory_hostname }} - Reason: {{ |
| 54 | + reboot_reason }}' |
45 | 55 | when: should_reboot |
46 | 56 |
|
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 | + |
54 | 78 | when: should_reboot |
| 79 | + ignore_errors: '{{ ansible_check_mode }}' |
0 commit comments