【问题标题】:Can ansible create a dict using the common values from a list and a dict?ansible 可以使用列表和字典中的公共值创建字典吗?
【发布时间】:2020-06-04 01:21:28
【问题描述】:

在 ansible 中,vmware_guest_info 模块会给我们一个虚拟机上的标签列表,但不包括关于这些标签的任何信息:

“标签”:[
“10.16.3”,
“迪克”,
“发展”
],

vmware_tag_info 模块为我们提供了一个带有这些标签信息的字典,包括描述和 ID,但不包括标签名称:

“10.16.3”:{
"tag_category_id": "urn:vmomi:InventoryServiceCategory:6eb9d643-8fa3-42a1-8b50-78a1c6e99867:GLOBAL",
"tag_description": "10.16.3",
"tag_id": "urn:vmomi:InventoryServiceTag:ca46ab80-be91-4c3a-8f9f-019d163dd954:GLOBAL",
"tag_used_by": []
},

vmware_category_info 模块为我们提供了一个包含标签 ID 和名称的列表。

“tag_category_info”:[
{
“category_associable_types”:[],
"category_cardinality": "SINGLE",
"category_description": "nodeVersion",
"category_id": "urn:vmomi:InventoryServiceCategory:6eb9d643-8fa3-42a1-8b50-78a1c6e99867:GLOBAL",
"category_name": "nodeVersion",
“category_used_by”:[]
},
]

所以看来我需要结合三个不同列表的输出来获取标签值、标签名称和标签ID。

我真的希望有人已经这样做了。如果没有,谁能解释一下如何迭代 vmware_tag_info 和 vmware_category_info 的输出,并找出 tag_category_id 何时与 category_id 匹配?

【问题讨论】:

    标签: ansible vmware vcenter


    【解决方案1】:

    这个怎么样?

    ---
    - name: Example playbook
      hosts: localhost
      gather_facts: no
      vars:
        vcenter_hostname: change me
        vcenter_username: administrator@vsphere.local
        vcenter_password: change me
        dc1: change me
        vm_name: change me
      tasks:
        - name: Gather tags information
          vmware_tag_info:
            hostname: "{{ vcenter_hostname }}"
            username: "{{ vcenter_username }}"
            password: "{{ vcenter_password }}"
            validate_certs: no
          register: tags_info_result
    
        - name: Gather tags category information
          vmware_category_info:
            hostname: "{{ vcenter_hostname }}"
            username: "{{ vcenter_username }}"
            password: "{{ vcenter_password }}"
            validate_certs: no
          register: tags_category_info_result
    
        - name: Set tags_information variable
          set_fact:
            tags_information: >-
              {{ tags_information | default({})
                | combine({
                    item.key: tags_category_info_result.tag_category_info
                    | selectattr('category_id', '==', item.value.tag_category_id)
                    | list
                    | first
                    | combine(item.value)
                    | combine({'tag_name': item.key})
                    })
              }}
          with_dict: "{{ tags_info_result.tag_facts }}"
    
        - name: Gather VM information
          vmware_guest_info:
            hostname: "{{ vcenter_hostname }}"
            username: "{{ vcenter_username }}"
            password: "{{ vcenter_password }}"
            validate_certs: no
            datacenter: "{{ dc1 }}"
            name: "{{ vm_name }}"
            tags: true
          register: vm_info_result
    
        - name: Display VM tags information
          debug:
            msg:
              - "tags information about {{ vm_name }}"
              - "{{ tags_information[item] }}"
          loop: "{{ vm_info_result.instance.tags }}"
          when:
            - "'tags' in vm_info_result.instance"
    

    【讨论】:

    • “收集 VM 信息”任务失败并显示以下消息:“信息收集失败,出现异常预期 VapiStruct 实例或 python 字典,但收到 str”我正在运行:@ 987654323@
    • 该错误是一个已知错误。这个bug已经修复了,你可以再试试下面的修复模块吗? github.com/ansible-collections/community.vmware/pull/406
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 2017-05-05
    • 2019-05-02
    相关资源
    最近更新 更多