从 json 获取值

Fetch values from json

我是新来的,也是 ansible 剧本的新手。我正在尝试获取我们的 F5 GTM/DNS 设备用于查询的所有 IP(不重要)。我在我的剧本中 运行 这个任务...

  tasks:
    - name: get gtm servers
      bigip_device_info:
        gather_subset: gtm-servers
        provider: "{{ providerA }}"
      register: out

下面是两个gtm-servers的输出,有很多。有两种不同类型的 gtm-servers,“product: bigip”和“product: generic-host”。如果是 bigip,我需要获取两个 IP(10.1.1.1 和 10.1.1.2)。如果它是通用主机,我不需要它。

{
"gtm_servers": [
    {
        "datacenter": "/Common/CACCO",
        "enabled": "yes",
        "expose_route_domains": "no",
        "iq_allow_path": "yes",
        "full_path": "/Common/CACCOIGW-APNADC009-10",
        "iq_allow_service_check": "yes",
        "iq_allow_snmp": "yes",
        "limit_cpu_usage": 0,
        "limit_cpu_usage_status": "no",
        "limit_max_bps": 0,
        "limit_max_bps_status": "no",
        "limit_max_connections": 0,
        "limit_max_connections_status": "no",
        "limit_max_pps": 0,
        "limit_max_pps_status": "no",
        "limit_mem_available": 0,
        "limit_mem_available_status": "no",
        "link_discovery": "disabled",
        "monitors": [
            "/Common/tcp_8080",
            "/Common/ping_monitor_5sec"
        ],
        "monitor_type": "and_list",
        "name": "CACCOIGW-APNADC009-10",
        "product": "bigip",
        "prober_fallback": "inherit",
        "prober_preference": "pool",
        "virtual_server_discovery": "disabled",
        "addresses": [
            {
                "name": "10.1.1.1",
                "deviceName": "caccoigw-apnadc009.na.xom.com",
                "translation": "none"
            },
            {
                "name": "10.1.1.2",
                "deviceName": "caccoigw-apnadc010.na.xom.com",
                "translation": "none"
            }
        ],
        "devices": [],
        "virtual_servers": []
    },
    {
        "datacenter": "/Common/USDAL",
        "enabled": "yes",
        "expose_route_domains": "no",
        "iq_allow_path": "yes",
        "full_path": "/Common/USDAL-GENHOST001",
        "iq_allow_service_check": "yes",
        "iq_allow_snmp": "yes",
        "limit_cpu_usage": 0,
        "limit_cpu_usage_status": "no",
        "limit_max_bps": 0,
        "limit_max_bps_status": "no",
        "limit_max_connections": 0,
        "limit_max_connections_status": "no",
        "limit_max_pps": 0,
        "limit_max_pps_status": "no",
        "limit_mem_available": 0,
        "limit_mem_available_status": "no",
        "link_discovery": "disabled",
        "monitors": [],
        "name": "USDAL-GENHOST001",
        "product": "generic-host",
        "prober_fallback": "inherit",
        "prober_preference": "inherit",
        "virtual_server_discovery": "disabled",
        "addresses": [
            {
                "name": "10.5.5.1",
                "deviceName": "USDAL-GENHOST001",
                "translation": "none"
            }
        ],
        "devices": [],
        "virtual_servers": []
    },

我已经试过了...

- name: fetch server ips
  set_fact:
    server_ips: "{{ out.gtm_servers | json_query('[*].{name: name}') }}"

但是当然它只会获取列表中的第一个名称,而不是包含 IP 的嵌套 addresses:name。而且我迷失了只获取 bigip IP 而不是通用主机 IP。非常感谢任何帮助。

例如

    - set_fact:
        server_ips: "{{ gtm_servers|
                        json_query('[?product==`bigip`].addresses[].name') }}"

给予

server_ips:
  - 10.1.1.1
  - 10.1.1.2