如果/ vmlinuz内核没有解析为uname -r,我正在使用Ansible检查主机是否需要重启.
即使测试计算机已重新启动并且内核正在解析到同一内核,if条件仍然始终标识重新启动:
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
您可以通过以下rpm查询可靠地确定最新安装的内核版本:
原文链接:https://www.f2er.com/centos/373644.htmlrpm -q kernel --queryformat '%{installtime} %{version}-%{release}.%{arch}\n' | sort -n -k1 | tail -1 | cut -d ' ' -f 2
RHEL 7上的示例输出:
3.10.0-229.11.1.el7.x86_64
现在只检查uname -r的输出是否匹配:
3.10.0-229.1.2.el7.x86_64
在此示例中,它不匹配,需要重新启动.
您可以使用test来比较字符串:
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