首页 > 解决方案 > Ansible - win_update 从输出中过滤 kb

问题描述

我们使用 ansible 和 win_update 模块更新我们的 Windows 服务器。现在我们要生成一个日志,其中包含哪个服务器下载了哪个更新(kb 编号)的信息。我可以成功地从输出中过滤出 kb 数字,但是当我将 kb 数字写入日志时,ansible 会为每个 kb 数字写入一个新行。所以它看起来像这样:

Host:ServerA ,IP:10.10.10.10 ,Downloaded Updates:2 ,KB-Number:[u4525236]
Host:ServerA ,IP:10.10.10.10 ,Downloaded Updates:2 ,KB-Number:[u2267602]

但我想要以下结果:

Host:ServerA ,IP:10.10.10.10 ,Downloaded Updates:2 ,KB-Number:[u4525236][u2267602]

我怎样才能做到这一点?

这是从 win_update 返回的示例:

[ServerA] => {
    "changed": false,
    "filtered_updates": {},
    "found_update_count": 2,
    "installed_update_count": 0,
    "reboot_required": false,
    "updates": {
        "1277d483-7f10-45e5-9037-739e191b6151": {
            "categories": [
                "Definition Updates",
                "Windows Defender"
            ],
            "id": "1277d483-7f10-45e5-9037-739e191b6151",
            "installed": false,
            "kb": [
                "2267602"
            ],
            "title": "Security Intelligence-Update für Windows Defender Antivirus - KB2267602 (Version 1.305.3389.0)"
        },
        "36511ef0-14b8-4883-a0bc-49c047981b50": {
            "categories": [
                "Security Updates",
                "Windows Server 2016"
            ],
            "id": "36511ef0-14b8-4883-a0bc-49c047981b50",
            "installed": false,
            "kb": [
                "4525236"
            ],
            "title": "2019-11 Kumulatives Update für Windows Server 2016 für x64-basierte Systeme (KB4525236)"
        }
    }
}

我的剧本中有以下任务来过滤 kb 并将其写入日志。我在“值”中注册了 win_update 返回。

- name: Creating Log
      shell: echo Host:{{inventory_hostname}} ,IP:{{ ansible_host }} ,Downloaded Updates:{{ values.found_update_count }} ,KB-Number:{{ item.value.kb }} >> log.csv
      with_items:
        - "{{ values.updates | dict2items }}"
      delegate_to: localhost

标签: ansible

解决方案


推荐阅读