ansible print key if inside value is defined

Viewed 67

can somebody please help me with this json parse? I have this json

{
"declaration": {
    "ACS-AS3": {
        "ACS": {
            "class": "Application",
            "vs_ubuntu_22": {
                "virtualAddresses": ["10.11.205.167"]
            },
            "pool_ubuntu_22": {
                "members": {
                    "addressDiscovery": "static",
                    "servicePort": 22
                }
            },

            "vs_ubuntu_443": {
                "virtualAddresses": ["10.11.205.167"],
                "virtualPort": 443
            },

            "pool_ubuntu01_443": {
                "members": [{
                    "addressDiscovery": "static",
                    "servicePort": 443,
                    "serverAddresses": [
                        "10.11.205.133",
                        "10.11.205.165"
                    ]
                }]
            },


            "vs_ubuntu_80": {
                "virtualAddresses": [
                    "10.11.205.167"
                ],
                "virtualPort": 80

            },
            "pool_ubuntu01_80": {
                "members": [{
                    "addressDiscovery": "static",
                    "servicePort": 80,
                    "serverAddresses": [
                        "10.11.205.133",
                        "10.11.205.165"
                    ],
                    "shareNodes": true
                }],
                "monitors": [{
                    "bigip": "/Common/tcp"
                }]

            }
        }
    }
}

}

and I am trying this playbook

    tasks:
 
  - name : deploy json file AS3  to F5
    debug:
      msg: "{{ lookup('file', 'parse2.json') }}"
    register: atc_AS3_status
    no_log: true

  
  - name : Parse json 1
    debug:
      var: atc_AS3_status.msg.declaration | json_query(query_result) | list
    vars: 
      query_result: "\"ACS-AS3\".ACS"
      #query_result1: "\"ACS-AS3\".ACS.*.virtualAddresses" 
    register: atc_AS3_status1

I got this response

TASK [Parse json 1] ******************************************************************************************************************************************************************************************
ok: [avx-bigip01.dhl.com] => {
    "atc_AS3_status1": {
        "atc_AS3_status.msg.declaration | json_query(query_result) | list": [
            "class",
            "vs_ubuntu_22",
            "pool_ubuntu_22",
            "vs_ubuntu_443",
            "pool_ubuntu01_443",
            "vs_ubuntu_80",
            "pool_ubuntu01_80"
        ],
        "changed": false,
        "failed": false
    }
}

but I would like to print just key which has inside key virtualAddresses if ""ACS-AS3".ACS.*.virtualAddresses" is defined the print the key .

the result should be vs_ubuntu_22 vs_ubuntu_443 vs_ubuntu_80

1 Answers

One way to get the keys of a dict, is to use the dict2items filter. This will give vs_ubuntu_22 etc. as "key" and their sub-dicts as "value". Using this we can conditionally check if virtualAddresses is defined in values.

Also parse2.json can be included as vars_file or with include_vars rather than having a task to debug and register the result.

Below task using vars_file in playbook should get you the intended keys from the JSON:

vars_files:
  - parse2.json

tasks:
  - name: show atc_status
    debug:
      var: item.key
    loop: "{{ declaration['ACS-AS3']['ACS'] | dict2items }}"
    when: item['value']['virtualAddresses'] is defined
Related