Ansible重启Debian/Ubuntu Linux进行内核更新并等待它
时间:2020-01-09 10:37:08 来源:igfitidea点击:
如何使用Ansible剧本远程重启Debian或Ubuntu Linux服务器/主机,以进行内核更新并等待其再次出现?
说明:使用Ansible IT自动化或DevOps工具更新大量云服务器或裸机服务器是相当标准的。
安装新内核后,必须重新启动Debian或Ubuntu Linux服务器。
该页面显示了如何使用Shell或命令模块重新引导计算机并等待其恢复。
您需要使用的Ansible模块
apt管理Debian/Ubuntu Linux的apt软件包,例如安装新软件包或更新软件包。command或shell使用shell模块在节点中执行命令。使用命令模块在远程节点上执行命令。内核更新后,可使用任何一个模块来重启盒子。wait_for_connection等待直到远程系统可用/可用。
Ansible重启Debian/Ubuntu Linux进行内核更新并等待它
让我们看看如何使用这些树型Ansible模块来重启Debian/Ubuntu Linux内核更新,并等待它再次恢复在线。
在Ansible中更新您的Debian或Ubuntu盒子
该剧本应如下:
- name: Update all packages
apt:
update_cache: yes
upgrade: dist
使Ansible等待服务器重新启动并继续将Playbook与Shell模块一起使用
如下更新剧本:
- name: Reboot box if kernel/libs updated and requested by the system
shell: sleep 10 && /sbin/shutdown -r now 'Rebooting box to update system libs/kernel as needed'
args:
removes: /var/run/reboot-required
async: 300
poll: 0
ignore_errors: true
如果Debian或Ubuntu Linux上存在文件/var/run/reboot-required,(请参阅如何确定Ubuntu/Debian Linux服务器是否需要重启),则需要使用shutdown/reboot重启Linux服务器。
命令。
请注意,我现在在shutdown -r之前添加了sleep命令,然后对shell模块使用async。
它强制剧本异步地执行shell模块。
"删除"是某种条件。
如果不存在名为/var/run/reboot-required的文件名,则将不会运行shutdown命令。
如果内核已更新,我们只需要运行reboot命令或shutdown命令。
重新启动并等待Ansible中的重新启动完成
等待的最后一个难题如下:
- name: Wait for system to become reachable again
wait_for_connection:
delay: 60
timeout: 300
现在,Ansible等待总计300秒。
我还增加了60秒等待开始轮询的时间。
使用Ansible Playbook远程重启主机
Ansible重启Debian/Ubuntu Linux进行内核更新并等待它完成示例:
# update.yml
--
- hosts: cluster
tasks:
- name: Update all packages on a Debian/Ubuntu
apt:
update_cache: yes
upgrade: dist
- name: Reboot box if kernel/libs updated and requested by the system
shell: sleep 10 && /sbin/shutdown -r now 'Rebooting box to update system libs/kernel as needed'
args:
removes: /var/run/reboot-required
async: 300
poll: 0
ignore_errors: true
- name: Wait for system to become reachable again
wait_for_connection:
delay: 60
timeout: 300
- name: Verify new update (optional)
command: uname -mrs
register: uname_result
- name: Display new kernel version
debug:
var: uname_result.stdout_lines
这是使用cat命令显示的我的主机文件:
[all:vars] ansible_user=Hyman ansible_port=22 ansible_python_interpreter='/usr/bin/env python3' ansible_become=yes ansible_become_method=sudo [cluster] mysql2 ansible_host=192.168.1.10 mysql1 ansible_host=192.168.1.11 mysql3 ansible_host=192.168.1.12
现在,如下所示:
$ ansible-playbook -i hosts update.yml

