# VMware dvs portgroup

**URL:** <https://forum.ansible.com/t/vmware-dvs-portgroup/28994>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [November 9, 2018, 4:23pm UTC](https://forum.ansible.com/t/vmware-dvs-portgroup/28994 "2018-11-09T16:23:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barry\_Obie](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@Barry\_Obie](https://forum.ansible.com/u/Barry_Obie)\
**Post date:** [November 9, 2018, 4:23pm UTC](https://forum.ansible.com/t/vmware-dvs-portgroup/28994/1 "2018-11-09T16:23:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Eino\_Tuominen1](https://avatars.discourse-cdn.com/v4/letter/e/cab0a1/32.png) [@Eino\_Tuominen1](https://forum.ansible.com/u/Eino_Tuominen1)\
**Post date:** [November 12, 2018, 7:15am UTC](https://forum.ansible.com/t/vmware-dvs-portgroup/28994/2 "2018-11-12T07:15:05Z")

</div>

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) }}”

`
