简体   繁体   English

如何为我的库存和 set_fact 中的每个主机选择嵌套的 json 值以供以后重用

[英]How to select a nested json value for each host in my inventory and set_fact for reuse later

I am fairly new to Ansible and using the vmware_guest_disk_info module to produce the following json:我对 Ansible 相当陌生,并使用 vmware_guest_disk_info 模块生成以下 json:

{
"msg": {
    "changed": false,
    "msg": "All items completed",
    "results": [
        {
            "ansible_loop_var": "result_item",
            "changed": false,
            "failed": false,
            "guest_disk_info": {
                "0": {
                    "capacity_in_bytes": 103079215104,
                    "capacity_in_kb": 100663296,
                    "controller_bus_number": 0,
                    "controller_key": 1000,
                    "controller_type": "lsilogicsas",
                    "key": 2000,
                    "label": "Hard disk 1",
                    "summary": "100,663,296 KB",
                    "unit_number": 0
                },
                "1": {
                    "capacity_in_bytes": 8589934592,
                    "capacity_in_kb": 8388608,
                    "controller_bus_number": 0,
                    "controller_key": 1000,
                    "controller_type": "lsilogicsas",
                    "key": 2001,
                    "label": "Hard disk 2",
                    "summary": "8,388,608 KB",
                    "unit_number": 1
                }
            },
            "invocation": {
                "module_args": {
                    "name": "server1",
                }
            },
            "result_item": {
                "vmname": "server1"
            }
        },
        {
            "ansible_loop_var": "result_item",
           "changed": false,
            "failed": false,
            "guest_disk_info": {
                "0": {
                    "capacity_in_bytes": 103079215104,
                    "capacity_in_kb": 100663296,
                    "controller_bus_number": 0,
                    "controller_key": 1000,
                    "controller_type": "lsilogicsas",
                    "key": 2000,
                    "label": "Hard disk 1",
                    "summary": "100,663,296 KB",
                    "unit_number": 0
                }
            },
            "invocation": {
                "module_args": {
                    "name": "server2",
                }
            },
            "result_item": {
                "vmname": "server2"
            }
        }
    ]
}

} }

I need to be able to extract the unit_number from each host and then reference each hosts own unit_number(s) later in another task(there can be multiple unit numbers per host).我需要能够从每个主机中提取 unit_number,然后在另一个任务中引用每个主机自己的 unit_number(s)(每个主机可以有多个单元号)。 It seems everything I tried just overwrites the previous value and I only end up with the last value of the last host.似乎我尝试的所有内容都覆盖了以前的值,而我最终只得到了最后一个主机的最后一个值。

I have tried:我努力了:

set_fact:
  facts: "{{ diskOutput.results.guest_disk_info.values()|map(attribute='unitNumber'} | list }}"

But I only get the last hosts data.但我只得到最后的主机数据。

Q: " Assign the next consecutive unused unit_number to the new disk. "问: 将下一个连续未使用的 unit_number 分配给新磁盘。

A: Create a dictionary of the servers and their units, eg A:创建服务器及其单位的字典,例如

    - set_fact:
        srv_units: "{{ dict(_servers|zip(_units)) }}"
      vars:
        _servers: "{{ diskOutput.results|
                      json_query('[].result_item.vmname') }}"
        _units: "{{ diskOutput.results|
                    json_query('[].guest_disk_info.*.unit_number') }}"

gives

  srv_units:
    server1:
    - 0
    - 1
    server2:
    - 0

Then iterate this dictionary and find the next units, eg然后迭代这个字典并找到下一个单位,例如

    - debug:
        msg: "{{ item.key }} next unit: {{ item.value|max + 1 }}"
      loop: "{{ srv_units|dict2items }}"

gives

  msg: 'server1 next unit: 2'
  msg: 'server2 next unit: 1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM