Search code examples
arraysansiblef5

Fetch values from json


I'm new here and new to ansible playbooks. I'm trying to grab all the IPs that our F5 GTM/DNS devices use for iquery (not important). I'm running this task in my playbook...

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

Below is the output of two of the gtm-servers, there are many. There are two different types of gtm-servers, "product: bigip" and "product: generic-host". If it's a bigip, I need to grab the two IPs (10.1.1.1 and 10.1.1.2). If it's a generic-host, I don't need it.

{
"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": []
    },

I've tried this...

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

But of course it only grabs the first name in the list and not that nested addresses:name that contains the IPs. And I'm lost on only grabbing the bigip IPs and not the generic-host IPs. Very grateful for any help.


Solution

  • For example

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

    gives

    server_ips:
      - 10.1.1.1
      - 10.1.1.2