From 991a810ac025206f2d1ca7669181d719d2fde929 Mon Sep 17 00:00:00 2001 From: Chris Hammer Date: Thu, 7 Nov 2024 16:50:09 -0500 Subject: [PATCH] Initial project commit --- .ansible-lint | 5 +++++ README.md | 6 ++++++ defaults/main.yml | 17 +++++++++++++++++ meta/main.yml | 26 ++++++++++++++++++++++++++ tasks/check_boot_id.yml | 38 ++++++++++++++++++++++++++++++++++++++ tasks/main.yml | 16 ++++++++++++++++ 6 files changed, 108 insertions(+) create mode 100644 .ansible-lint create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/check_boot_id.yml create mode 100644 tasks/main.yml diff --git a/.ansible-lint b/.ansible-lint new file mode 100644 index 0000000..b6d3809 --- /dev/null +++ b/.ansible-lint @@ -0,0 +1,5 @@ +skip_list: + - yaml[colons] + - yaml[empty-lines] + - yaml[line-length] + - no-changed-when diff --git a/README.md b/README.md new file mode 100644 index 0000000..8d1275a --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +verified_reboot +========= + +This role will use proc's boot_id file to determine if a host has rebooted or not. + + diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..e3b6fce --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,17 @@ +--- +verified_reboot_reboot_time: 1 # Time in minutes issued to the shutdown command +verified_reboot_reboot_msg: '*** ANSIBLE INITIATED REBOOT ***' +verified_reboot_bootid_file: /proc/sys/kernel/random/boot_id +verified_reboot_max_checks: 10 + +# Maximum number of seconds to wait for a connection to happen before closing and retrying. +verified_reboot_wait_conn_timeout: 20 + +# Number of seconds to sleep between checks. +verified_reboot_wait_sleep: 10 + +# Number of seconds to wait before starting to poll. +verified_reboot_wait_delay: 70 + +# Maximum number of seconds to wait for. +verified_reboot_wait_timeout: 1800 diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..0237532 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,26 @@ +galaxy_info: + author: Chris Hammer + description: This role will use proc's boot_id file to determine if a host has rebooted or not. + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: BSD-3-Clause + + min_ansible_version: "2.14" + + platforms: + - name: EL + versions: + - "7" + - "8" + - "9" + diff --git a/tasks/check_boot_id.yml b/tasks/check_boot_id.yml new file mode 100644 index 0000000..a926a4b --- /dev/null +++ b/tasks/check_boot_id.yml @@ -0,0 +1,38 @@ +--- +- name: Wait for connection to host + ansible.builtin.wait_for_connection: + connect_timeout: "{{ verified_reboot_wait_conn_timeout }}" + sleep: "{{ verified_reboot_wait_sleep }}" + delay: "{{ verified_reboot_wait_delay }}" + timeout: "{{ verified_reboot_wait_timeout }}" + +- name: Increment check count + ansible.builtin.set_fact: + verified_reboot_check_count: + "{{ 1 if verified_reboot_check_count is undefined else verified_reboot_check_count | int + 1 }}" + +- name: Capture post-reboot boot ID + ansible.builtin.slurp: + src: "{{ verified_reboot_bootid_file }}" + register: verified_reboot_post_boot_id_raw + +- name: Set post-reboot boot ID + ansible.builtin.set_fact: + verified_reboot_post_boot_id: "{{ verified_reboot_post_boot_id_raw['content'] | b64decode | trim }}" + +- name: Debug boot IDs + ansible.builtin.debug: + msg: "{{ verified_reboot_pre_boot_id }} == {{ verified_reboot_post_boot_id }}" + verbosity: 1 + +- name: Max check count exception + ansible.builtin.fail: + msg: "Max check count ({{ verified_reboot_max_checks }}) reached. Aborting." + when: + - verified_reboot_check_count | int >= verified_reboot_max_checks + +- name: Re-check host reboot status + ansible.builtin.include_tasks: check_boot_id.yml + when: + - verified_reboot_pre_boot_id == verified_reboot_post_boot_id + - verified_reboot_check_count | int < verified_reboot_max_checks diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..6458209 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,16 @@ +--- +- name: Capture initial boot ID + ansible.builtin.slurp: + src: "{{ verified_reboot_bootid_file }}" + register: verified_reboot_pre_boot_id_raw + +- name: Set pre-reboot boot ID + ansible.builtin.set_fact: + verified_reboot_pre_boot_id: "{{ verified_reboot_pre_boot_id_raw['content'] | b64decode | trim }}" + +- name: Reboot the host + ansible.builtin.command: + cmd: "/usr/sbin/shutdown -r +{{ verified_reboot_reboot_time }} '{{ verified_reboot_reboot_msg }}'" + +- name: Verify reboot status of host + ansible.builtin.include_tasks: check_boot_id.yml