Hi,

I would suggest the following solution to extract the required information:

- hosts: 127.0.0.1
  connection: local
  vars:
      dvs: "{{ lookup('file', 'result.json') }}"
  tasks:
  - name: Simple debug.
    debug:
      msg: "{{ dvs.dvs_portgroup_info[\"DVS-Dev-Ork2\"] }}"
    tags:
      - G2
# VERSION 1
  - name: Extract portgroup info
    set_fact:
      port_groups: "{{ dvs | json_query(query) }}"
    vars:
      query: |
        dvs_portgroup_info.*[].{
            portgroup_name: portgroup_name,
            vlan_id: vlan_info.vlan_id
        }

  - debug:
      var: port_groups

# VERSION 2
  - name: Extract portgroups alternative
    set_fact:
        port_groups_alt: "{{ (port_groups_alt | default([])) + [port_group] 
}}"
    vars:
        port_group:
          portgroup_name: "{{ item.portgroup_name }}"
          vlan_id: "{{ item.vlan_info.vlan_id }}"
    loop: "{{ dvs.dvs_portgroup_info['DVS-Dev-Ork2'] }}"
    loop_control:
      label: "{{ item.portgroup_name }}"

  - debug:
      var: port_groups_alt

This playbook contains two alternative attempts. The first is using 
JMESPath via json_query and is a more declarative approach, if you will. 
The second is a plain looped set_fact.

Both have the same result: a list of dictionaries with portgroup_name and 
vlan_id:

TASK [Extract portgroup info] 
**************************************************************************************************************************************************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [debug] 
*******************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "port_groups": [
        {
            "portgroup_name": "Test - 192.168.20.0_24 - VMotion - 1",
            "vlan_id": "20"
        },
        {
            "portgroup_name": "Test - 192.168.60.0_24 - Management",
            "vlan_id": "0"
        },
        {
            "portgroup_name": "Test - 192.168.30.0_24 - VSAN",
            "vlan_id": "25"
        },
        {
            "portgroup_name": "DVS-Dev-Ork2-DVUplinks-11",
            "vlan_id": [
                "0-4094"
            ]
        }
    ]
}

TASK [Extract portgroups alternative] 
******************************************************************************************************************************************************************************************************************************************************************************************
ok: [127.0.0.1] => (item=Test - 192.168.20.0_24 - VMotion - 1)
ok: [127.0.0.1] => (item=Test - 192.168.60.0_24 - Management)
ok: [127.0.0.1] => (item=Test - 192.168.30.0_24 - VSAN)
ok: [127.0.0.1] => (item=DVS-Dev-Ork2-DVUplinks-11)

TASK [debug] 
*******************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "port_groups_alt": [
        {
            "portgroup_name": "Test - 192.168.20.0_24 - VMotion - 1",
            "vlan_id": "20"
        },
        {
            "portgroup_name": "Test - 192.168.60.0_24 - Management",
            "vlan_id": "0"
        },
        {
            "portgroup_name": "Test - 192.168.30.0_24 - VSAN",
            "vlan_id": "25"
        },
        {
            "portgroup_name": "DVS-Dev-Ork2-DVUplinks-11",
            "vlan_id": [
                "0-4094"
            ]
        }
    ]
}

The json_query version would even work, if there are more than one object 
in dvs.dvs_portgroup_info.
The last entry may need additional handling as it contains a list of 
vlan_ids.

Cheers,
André

On Thursday, May 20, 2021 at 8:49:44 AM UTC+2 bugp...@gmail.com wrote:

> Hi Vladimir,
>
> Thanks for your help! Basically I'm looking to gather portgroup_name and 
> vlanid to use on the next task. What will be the best way to proceed?
>
> On Thursday, 20 May 2021 at 05:16:26 UTC+1 vbo...@gmail.com wrote:
>
>> On Wed, 19 May 2021 16:17:17 -0700 (PDT)
>> Rodrigues - <bugp...@gmail.com> wrote:
>>
>> > ... get the portgroups
>> > 
>> > - name: Simple debug.
>> > debug:
>> > msg: "{{ dvs.dvs_portgroup_info[\"DVS-Dev-Ork2\"] }}"
>>
>> What result do you expect?
>>
>> The attribute *DVS-Dev-Ork2* is a list of dictionaries. There are
>> many options on how to proceed. For example, you can iterate the list
>> and display *portgroups* along with the switch name
>>
>> - debug:
>> msg: "{{ item.dvswitch_name }} {{ item.key }}"
>> loop: "{{ dvs.dvs_portgroup_info['DVS-Dev-Ork2'] }}
>>
>> Or, you can create a list of *portgroups*, e.g.
>>
>> - set_fact:
>> portgroups: "{{ dvs.dvs_portgroup_info['DVS-Dev-Ork2']|
>> map(attribute='key')|list }}"
>>
>>
>> -- 
>> Vladimir Botka
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/0c75c2a1-cf0a-46fb-9781-f17c7a91005cn%40googlegroups.com.

Reply via email to