1

When I run the following task:

- name: Get info about DVPG
  community.vmware.vmware_dvs_portgroup_info:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    datacenter: "{{ datacenter_name }}"
    validate_certs: no
  register: dvpg_var

It returns the following result:

{
   "dvpg_var":{
      "changed":false,
      "dvs_portgroup_info":{
         "WIFI-NEPTUNE":[
            {
               "active_uplinks":[
                  "lag1"
               ],
               "description":null,
               "dvswitch_name":"WIFI-NEPTUNE",
               "key":"dvportgroup-3000",
               "mac_learning":{
                  "allow_unicast_flooding":null,
                  "enabled":false,
                  "limit":null,
                  "limit_policy":null
               },
               "network_policy":{
                  "forged_transmits":true,
                  "mac_changes":true,
                  "promiscuous":false
               },
               "num_ports":4,
               "port_allocation":"elastic",
               "port_binding":"static",
               "port_policy":{
                  "block_override":true,
                  "ipfix_override":false,
                  "live_port_move":false,
                  "network_rp_override":false,
                  "port_config_reset_at_disconnect":true,
                  "security_override":false,
                  "shaping_override":false,
                  "traffic_filter_override":false,
                  "uplink_teaming_override":false,
                  "vendor_config_override":false,
                  "vlan_override":false
               },
               "portgroup_name":"VLAN100",
               "standby_uplinks":[
                  
               ],
               "teaming_policy":{
                  "inbound_policy":true,
                  "notify_switches":true,
                  "policy":"loadbalance_srcid",
                  "rolling_order":false
               },
               "type":"earlyBinding",
               "vlan_info":{
                  
               }
            }
         ],
         "WIFI-SATURNO":[
            {
               "active_uplinks":[
                  "lag2"
               ],
               "description":null,
               "dvswitch_name":"WIFI-SATURNO",
               "key":"dvportgroup-3001",
               "mac_learning":{
                  "allow_unicast_flooding":null,
                  "enabled":false,
                  "limit":null,
                  "limit_policy":null
               },
               "network_policy":{
                  "forged_transmits":true,
                  "mac_changes":true,
                  "promiscuous":false
               },
               "num_ports":4,
               "port_allocation":"elastic",
               "port_binding":"static",
               "port_policy":{
                  "block_override":true,
                  "ipfix_override":false,
                  "live_port_move":false,
                  "network_rp_override":false,
                  "port_config_reset_at_disconnect":true,
                  "security_override":false,
                  "shaping_override":false,
                  "traffic_filter_override":false,
                  "uplink_teaming_override":false,
                  "vendor_config_override":false,
                  "vlan_override":false
               },
               "portgroup_name":"VLAN101",
               "standby_uplinks":[
                  
               ],
               "teaming_policy":{
                  "inbound_policy":true,
                  "notify_switches":true,
                  "policy":"loadbalance_srcid",
                  "rolling_order":false
               },
               "type":"earlyBinding",
               "vlan_info":{
                  
               }
            }
         ]
      }
   }
}

I'm unable to find a way to iterate, loop or only retrieve the portgroup_name values.

I tried to do this:

- name: Get portgroup_name list
  debug:
    msg: "{{ dvpg_var.dvs_portgroup_info['WIFI-NEPTUNE'] | json_query('[*].portgroup_name') }}"

It works, but it's not what I need. Instead of specifying dvpg_var.dvs_portgroup_info['WIFI-NEPTUNE'], I want to iterate over the keys under dvpg_var.dvs_portgroup_info.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Bizargorri
  • 11
  • 3

1 Answers1

0

You can use JMESPath — the query language used by json_query — in order to give a wildcard for the dictionary key under dvs_portgroup_info:

- debug:
    var: dvpg_var.dvs_portgroup_info | json_query('*[*].portgroup_name[]')

Note: the last [] is actually the flatten operator of JMESPath and is there to flatten the list of list created by the lists under WIFI-NEPTUNE and WIFI-SATURNO.


Given the task:

- debug:
    var: dvpg_var.dvs_portgroup_info | json_query('*[*].portgroup_name[]')
  vars:
    dvpg_var:
      dvs_portgroup_info:
        WIFI-NEPTUNE:
          - portgroup_name: VLAN100
        WIFI-SATURNO:
          - portgroup_name: VLAN101

This yields

ok: [localhost] => 
  dvpg_var.dvs_portgroup_info | json_query('*[*].portgroup_name[]'):
  - VLAN100
  - VLAN101
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • I can't believe, it works perfectly. You are a magician @β.εηοιτ.βε, thans you very much. Could you recommend tips, web pages, documentation to improve my understanding of this topic? – Bizargorri May 17 '23 at 06:45
  • @Bizargorri If you are speaking strictly about JMESPath, their tutorial is quite nice, each examples query and input are editable and you can play with the value to see how the result adapt. https://jmespath.org/tutorial.html – β.εηοιτ.βε May 17 '23 at 19:43