VMware dvs portgroup

Hi. I’m trying to use the VMware module to configure my vCenters.
I have over 100 vlans and roughly 30 dv switches that I’m trying to configure. The dvs switches could have any combination of the vlans. I could do something like

`

  • vlanid: vlan1
    vlan: 1
    dvs: DSwitch1
    numports: 128

  • vlanid: vlan2
    vlan: 2
    dvs: DSwitch1
    numports: 128

  • vlanid: vlan1
    vlan: 1
    dvs: DSwitch2
    numports: 128

  • vlanid: vlan2
    vlan: 2
    dvs: DSwitch2
    numports: 128
    `

But then I end up having a ridiculous list that has stuff duplicated all over the place.
Is there a way to do something like:
`

  • vlanid: vlan1
    vlan: 1
    dvs:
  • DSwitch1
  • DSwitch2
    numports: 128
  • vlanid: vlan2
    vlan: 2
    dvs:
    -DSwitch2
    numports: 128
    `

Or another way to be able to do that? This is an issue all over my vCenter config playbook, not just vlans. So finding a better way than building a long dictionary would be ideal. I imagine I’d need to change the way the loop works, but I’m not sure what road to go down.
`
Playbook.yml

  • name: Create DVS portgroup
    vmware_dvs_portgroup:
    hostname: “{{secret.vcenter}}”
    username: “{{secret.vcusername}}”
    password: “{{secret.vcpassword}}”
    validate_certs: no
    portgroup_name: “{{item.vlanid}}”
    vlan_id: “{{item.vlan}}”
    portgroup_type: earlyBinding
    num_ports: “{{item.numports}}”
    state: present
    switch_name: “{{item.dvs}}”
    loop: “{{config.vlans}}”
    `

Thanks!

Barry

Hi Barry,

Ansible 2.7 has subelements filter, which makes this rather easy.

Given your second struct:

`
vars:
vmds:

  • vlanid: vlan1
    vlan: 1
    dvs:
  • DSwitch1
  • DSwitch2
    numports: 128
  • vlanid: vlan2
    vlan: 2
    dvs:
  • DSwitch2
    numports: 128

`

You can loop over the vswitches with the help of the subelements filter:

`

  • debug:
    var: vlans
    vars:
    vlans: “{{ vmds | subelements(‘dvs’, skip_missing=True) }}”

`

So, you should be able to create the port groups like this:​

`

  • name: Create DVS portgroup

vmware_dvs_portgroup:
hostname: “{{secret.vcenter}}”
username: “{{secret.vcusername}}”
password: “{{secret.vcpassword}}”
validate_certs: no
portgroup_name: “{{ item.0.vlanid }}”
vlan_id: “{{item.0.vlan}}”
portgroup_type: earlyBinding
num_ports: “{{item.0.numports}}”
state: present
switch_name: “{{item.1}}”
loop: “{{ vmds | subelements(‘dvs’, skip_missing=True) }}”

`