我有一个简单的 Ansible 剧本,用于在我管理的所有服务器上运行更新:
- hosts: ubuntu
tasks:
- name: install all updates
apt:
upgrade: dist
update_cache: yes
autoremove: yes
autoclean: yes
- hosts: centos
tasks:
- name: install all updates
yum:
name: '*'
update_cache: yes
state: latest
# use debug to show the output
register: result
- name: Show Output
debug: msg="{{ result.stdout_lines }}"
有什么方法可以让 Ansible 显示哪些软件包在此过程中得到更新吗?易于也不是好吃模块为此提供了一个选项。
当前使用的 Ansible 版本是 2.4。
答案1
从 HBruijn 的评论开始,我扩展了我的剧本以显示随后的包管理日志的结果:
---
- hosts: ubuntu
tasks:
- name: install all updates
apt:
upgrade: dist
update_cache: yes
autoremove: yes
autoclean: yes
register: result
- name: List installed and updated packages
shell: grep -E "^$(date +%Y-%m-%d).+ (install|upgrade) " /var/log/dpkg.log |cut -d " " -f 3-5
register: result
- name: Show Output
debug: msg="{{ result.stdout_lines }}"
- hosts: centos
tasks:
- name: install all updates
yum:
name: '*'
update_cache: yes
state: latest
- name: List updated packages
shell: rpm -qa --last | grep "$(date +%a\ %d\ %b\ %Y)" |cut -f 1 -d " "
register: result
args:
warn: no
- name: Updates packages
debug: msg="{{ result.stdout_lines }}"
输出结果:
ok: [ubuntu-host] => {
"msg": [
"upgrade python3-problem-report:all 2.14.1-0ubuntu3.25",
"upgrade python3-apport:all 2.14.1-0ubuntu3.25",
"upgrade apport:all 2.14.1-0ubuntu3.25",
"upgrade firefox:i386 56.0+build6-0ubuntu0.14.04.2",
"upgrade python-problem-report:all 2.14.1-0ubuntu3.25",
"upgrade python-apport:all 2.14.1-0ubuntu3.25",
"upgrade xul-ext-ubufox:all 3.4-0ubuntu0.14.04.1"
]
}
ok: [centos-host] => {
"msg": [
"kernel-headers-2.6.32-696.16.1.el6.x86_64",
"lvm2-2.02.143-12.el6_9.1.x86_64",
"device-mapper-multipath-0.4.9-100.el6_9.1.x86_64",
"kernel-2.6.32-696.16.1.el6.x86_64",
"kernel-firmware-2.6.32-696.16.1.el6.noarch",
"lvm2-libs-2.02.143-12.el6_9.1.x86_64",
"kpartx-0.4.9-100.el6_9.1.x86_64",
"device-mapper-multipath-libs-0.4.9-100.el6_9.1.x86_64",
"device-mapper-event-libs-1.02.117-12.el6_9.1.x86_64",
"device-mapper-event-1.02.117-12.el6_9.1.x86_64",
"device-mapper-1.02.117-12.el6_9.1.x86_64",
"util-linux-ng-2.17.2-12.28.el6_9.1.x86_64",
"device-mapper-libs-1.02.117-12.el6_9.1.x86_64",
"libblkid-2.17.2-12.28.el6_9.1.x86_64",
"libuuid-2.17.2-12.28.el6_9.1.x86_64"
]
}
这是一个巨大的进步,但我仍然希望有人有更好的解决方案。
答案2
我采用了 Gerald Schneider 的基本方法,并添加了一些条件,以便仅在发生更改时检查更新。这解决了常见情况下的问题,尽管如果每天发生多次更新,它仍会产生一些无关的输出。
- hosts: debian
become: yes
tasks:
- name: Debian/Raspbian Update
apt:
update_cache: true
upgrade: yes
register: debian
#- debug:
# msg: " {{ debian.stdout_lines }} "
- name: List installed and updated packages
shell: grep -E "^$(date +%Y-%m-%d).+ (install|upgrade) " /var/log/dpkg.log |cut -d " " -f 3-5
register: result
when: debian.changed
- name: Show Output
debug: msg="{{ result.stdout_lines }}"
when: debian.changed
生成类似的输出:
TASK [Debian/Raspbian Update] ***************************************************************************************************************************************************************************************************************************************
ok: [clusterpi-01.local]
ok: [clusterpi-00.local]
ok: [clusterpi-03.local]
ok: [clusterpi-02.local]
ok: [radpi.local]
ok: [firefly]
ok: [blueberrypi.local]
changed: [blackberrypi.local]
changed: [snozzberrypi.local]
ok: [pizero]
TASK [List installed and updated packages] **************************************************************************************************************************************************************************************************************************
skipping: [radpi.local]
skipping: [pizero]
skipping: [blueberrypi.local]
skipping: [clusterpi-00.local]
skipping: [clusterpi-01.local]
skipping: [clusterpi-02.local]
skipping: [clusterpi-03.local]
skipping: [firefly]
changed: [blackberrypi.local]
changed: [snozzberrypi.local]
TASK [Show Output] **************************************************************************************************************************************************************************************************************************************************
skipping: [radpi.local]
ok: [snozzberrypi.local] => {
"msg": [
"upgrade rpi-chromium-mods:armhf 20190218",
"upgrade wiringpi:armhf 2.46"
]
}
skipping: [pizero]
ok: [blackberrypi.local] => {
"msg": [
"upgrade rpi-chromium-mods:armhf 20190218",
"upgrade wiringpi:armhf 2.46"
]
}
skipping: [blueberrypi.local]
skipping: [clusterpi-00.local]
skipping: [clusterpi-01.local]
skipping: [clusterpi-02.local]
skipping: [clusterpi-03.local]
skipping: [firefly]
也可以轻松为基于 dnf/yum 的发行版添加类似的测试。