AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 883314
Accepted
Gerald Schneider
Gerald Schneider
Asked: 2017-11-14 23:15:02 +0800 CST2017-11-14 23:15:02 +0800 CST 2017-11-14 23:15:02 +0800 CST

使用 Ansible 包管理显示更新的包

  • 772

我有一个简单的 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 向我展示在此过程中更新了哪些包?apt和yum模块都没有为此提供选项。

目前使用的 Ansible 版本是 2.4。

ansible
  • 2 2 个回答
  • 14211 Views

2 个回答

  • Voted
  1. Best Answer
    Gerald Schneider
    2017-11-17T01:24:15+08:002017-11-17T01:24:15+08:00

    从 HBrijn 的评论开始,我扩展了我的剧本以显示包管理日志的结果:

    ---
    
    - 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"
        ]
    }
    

    这是一个巨大的改进,但我仍然希望有人有更好的解决方案。

    • 9
  2. Eric Koldinger
    2019-03-14T15:04:09+08:002019-03-14T15:04:09+08:00

    我采用了 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 的发行版中。

    • 2

相关问题

  • 重复的 Ansible 任务

  • 无法形成站点中的文件的链接,该链接可用于使用 ansible 在远程服务器中启用的目录站点?

  • 如何执行 ansible 的特定角色?

  • Ansible 和 rbash

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve