Compare commits

...

2 Commits

Author SHA1 Message Date
f5c6d28095 Refactor to be a bit more modular 2026-03-11 14:15:40 -04:00
601954fe7c Refactor to be a bit more modular 2026-03-11 14:15:04 -04:00
3 changed files with 51 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
- name: Create test script to determine if reboot is necessary
hosts: all
hosts: arch debian firewall
tasks:
- name: Create ~/bin if it doesn't already exist
ansible.builtin.shell:
@@ -12,11 +12,11 @@
print '#!/usr/bin/env zsh' > needs_reboot
source ~/.zsh_functions
declare -f kernel_func >> needs_reboot
perl -pi -e 'if (/OK/) { $_ = "false\n" }' needs_reboot
perl -pi -e 'if (/needs reboot/) { $_ = "true\n" }' needs_reboot
perl -pi -e 'if (/OK/) { $_ = "\t\techo no\n" }' needs_reboot
perl -pi -e 'if (/needs reboot/) { $_ = "\t\techo yes\n" }' needs_reboot
printf "\n\n\nkernel_func\n" >> needs_reboot
chmod +x needs_reboot
args:
creates: needs_reboot
#creates: needs_reboot
executable: /usr/bin/zsh
chdir: ~/bin/

View File

@@ -1,13 +1,39 @@
kernel_func () {
machine_id=$(cat /etc/machine-id)
distro="$(awk -F= '/^ID/ {print $NF}' /etc/os-release)"
if [[ "${distro}" == arch ]]
then
package="$(pacman -Q \
| grep -vE -- '-(firmware|api|keyring|linux|docs|headers)' \
| grep linux | awk '{print $1}')"
current_kernel=$(uname -r)
next_kernel="$(pacman -Q | grep "${package}" \
| awk '{print $NF}' | tail -1 | tr -d '\n')"
current_ucode=$(awk -F'[[:space:]]*:[[:space:]]*' '/microcode/ {print $2}' /proc/cpuinfo | uniq)
[[ -f /run/next_kernel ]] && next_kernel="$(sudo cat /run/next_kernel)"
if whence iucode_tool &> /dev/null
then
next_ucode=$(iucode_tool -lqS /lib/firmware/intel-ucode/ | grep -Po 'rev 0x\d+' | tr -d '[rev ]' | tail -1)
if [[ "${current_kernel}" == ${next_kernel} ]] && [[ "${current_ucode}" == ${next_ucode} ]] || [[ -z "${next_kernel}" ]]
fi
if [[ "${current_kernel}" =~ ${next_kernel} ]]
then
if [[ -n "${next_ucode}" ]] && [[ "${current_ucode}" == ${next_ucode} ]]
then
print -P "[%F{#00ff00}OK%f]"
else
print -P "[%F{yellow}needs reboot%f]"
fi
else
print -P "[%F{yellow}needs reboot%f]"
fi
elif [[ "${distro}" == debian ]]
then
current_kernel="$(uname -v | awk '{print $5}')"
next_kernel="$(dpkg -l | grep -P '^ii\s+linux-image' | awk '{print $3}' \
| sort -Vu | tail -1)"
if [[ "${current_kernel}" == ${next_kernel} ]]
then
print -P "[%F{#00ff00}OK%f]"
else
print -P "[%F{yellow}needs reboot%f]"
fi
fi
}

View File

@@ -1,13 +1,19 @@
- name: Stop mollyguard if active
ansible.builtin.script: stop_mollyguard
register: mg
- name: Determine if a reboot is necessary
ansible.builtin.command:
cmd: ~/bin/needs_reboot
register: needs_reboot
- name: Stop mollyguard if active
become: true
ansible.builtin.script: stop_mollyguard
register: mg
- name: Print mg dict
ansible.builtin.debug:
var: mg
when: needs_reboot.stdout == "yes"
- name: Conditionally reboot
become: true
ansible.builtin.reboot:
when:
- inventory_hostname not in group['controller']
- mgc succeeded
- needs_reboot succeeded
- inventory_hostname not in groups['control']
- mg.failed is false
- needs_reboot.stdout == "yes"