问题原文
原帖链接 · nicoX · CC BY-SA 3.0
I'm using Ansible to check hosts if they need to be rebooted if the /vmlinuz kernel doesn't resolve to uname -r.
The if condition is although always identifing a reboot even though the test machine has been rebooted and the kernel is resolving to the same kernel:
if [ $(readlink -f /vmlinuz) != /boot/vmlinuz-$(uname -r) ]; then echo 'reboot'; else echo 'no'; fi
- name: Check for reboot hint.
shell: if [ $(readlink -f /vmlinuz) != /boot/vmlinuz-$(uname -r) ]; then echo 'reboot'; else echo 'no'; fi
ignore_errors: true
register: reboot_hint
- name: Rebooting ...
command: shutdown -r now "Ansible kernel update applied"
async: 0
poll: 0
ignore_errors: true
when: kernelup|changed or reboot_hint.stdout.find("reboot") != -1
register: rebooting
- name: Wait for thing to reboot...
pause: seconds=45
when: rebooting|changed
社区高分解答
原帖链接 · Reiner Rottmann · CC BY-SA 3.0
You could reliably determine the latest installed kernel version via the following rpm query:
rpm -q kernel --queryformat '%{installtime} %{version}-%{release}.%{arch}\n' | sort -n -k1 | tail -1 | cut -d ' ' -f 2
Sample output on RHEL 7:
3.10.0-229.11.1.el7.x86_64
Now just check whether the output of uname -r matches:
3.10.0-229.1.2.el7.x86_64
In this example, it does not match and a reboot is required.
You could use test to compare the strings:
if [ "`rpm -q kernel --queryformat '%{installtime} %{version}-%{release}.%{arch}\n' | sort -n -k1 | tail -1 | cut -d ' ' -f 2`" = "`uname -r`" ]; then echo "latest kernel already booted."; else echo "new kernel. reboot required."; fi
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
