首页 > 解决方案 > Ansible如何获取windows更新kb数

问题描述

我正在尝试使用 ansible 获取挂起的已安装 Windows 更新 kb。

       - name: Check for missing updates.
         win_updates:
           state: searched
           category_names: "{{ win_updates_categories }}"
         register: update_count
         ignore_errors: yes
    
    
       - debug: msg="{{ update_count.updates.kb }}"

但是运行出错,谁能帮帮我,谢谢!

这是寄存器更新的输出

  - debug: 
      var: update_count

   "update_count": {
        "changed": false, 
        "failed": false, 
        "filtered_updates": {}, 
        "found_update_count": 1, 
        "installed_update_count": 0, 
        "reboot_required": false, 
        "updates": {
            "67eab6a6-099b-42c5-86ce-63681f58ebd2": {
                "categories": [
                    "Security Updates", 
                    "Windows Server 2016"
                ], 
                "id": "67eab6a6-099b-42c5-86ce-63681f58ebd2", 
                "installed": false, 
                "kb": [
                    "4593226"
                ], 
               
            }
        }
    }
}

如果我只想显示 kb 信息,这是错误

  - debug: 
      var: update_count.updates.kb
    "update_count.updates.kb": "VARIABLE IS NOT DEFINED!"

标签: ansible

解决方案


请看下面,我使用 {{ item.value.id }} 来获取 ID 值(最后一个任务),如果它们不止一个,则将它们保存在一个数组中,同样的方式你应该能够检索 KB。希望这能解释

仅安装关键更新 - 必要时重新启动

      - name: install category critical updates only
        win_updates: 
         category_names: ['CriticalUpdates']
         server_selection: windows_update
         state: installed
         
        ignore_errors: yes
        register: CriticalUpdateResults

      - name: Postgres Column Values assigned
        set_fact: UpdateCategory="{{ CriticalUpdateResults }}"

      - name: CriticalUpdates Category assigned
        set_fact: PatchCategory="CriticalUpdates"
      - name: UpdatesArray assigned space
        set_fact: 
         UpdatesArray: ""

      - name: UpdatesArray assigned None if no update count
        set_fact: 
         UpdatesArray: "None"
        when: UpdateCategory.found_update_count == 0


      - name: Updates Array assigned
        set_fact: 
         UpdatesArray: "{{ UpdatesArray }} {{ **item.value.id** }}"
        with_items:
         - "{{ UpdateCategory.updates | dict2items }}"
        when: UpdateCategory.found_update_count != 0 

推荐阅读