VMware dvs portgroup

10 views
Skip to first unread message

Barry Obie

unread,
Nov 9, 2018, 11:23:06 AM11/9/18
to Ansible Project
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

Eino Tuominen

unread,
Nov 12, 2018, 2:15:17 AM11/12/18
to Ansible Project

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


-- 

  Eino Tuominen



From: Barry Obie <barry...@gmail.com>
Sent: Friday, November 9, 2018 18:23
To: Ansible Project
Subject: [ansible-project] VMware dvs portgroup
 
--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/da811cb6-9cd0-4011-8c8d-f293124c731a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages