Cumulative group variables?

72 views
Skip to first unread message

Cody John

unread,
Oct 24, 2016, 3:22:53 PM10/24/16
to Ansible Project
I'm working on modeling our datacenter top-of-rack switches in Ansible, and have run into a potential problem with my approach.   The basic idea is to use a hierarchal group structure that starts with general purpose (tor), and then get more specific for each deployment based on application that rack is supporting:

[tors]

[app1switches]
app1switch1
app1switch2

[app2switches]
app2switch1
app2switch2

[tors:children]
app1switches
app2switches


I want to abstract all my VLAN configuration into group-defined variables.  In this example, let's say that there is at least one VLAN that is standard across all TORs, and then each application has their own unique set of VLANs that also need to be applied.   My idea was to create group variable files such as:

#
# tors
#
---
vlans
:
 
- name: GLOBAL-VLAN
   id
: 100
   net
: "{{ globalvlan_prefix }}"



#
# app1switches
#
---
vlans
:
 
- name: SPECIAL-APP1-VLAN
   id
: 110
   net
: "{{ app1_prefix }}"


#
# app2switches
#
---
vlans
:
 
- name: SPECIAL-APP2-VLAN
   id
: 120
   net
: "{{ app2_prefix }}"


Ultimately the goal is for any device that is a member of the app1switches group to get both SPECIAL-APP1-VLAN + GLOBAL-VLAN, while app2switches would get SPECIAL-APP2-VLAN + GLOBAL-VLAN.  The problem is, the more specific group variable is over-riding the more generic so I am only applying the special app vlans.   

Is there a way to define these list variables in an cumulative way, such that the end-result of the 'vlans' variable is a 2 item list?

--
Cody
 
Message has been deleted

Mike Biancaniello

unread,
Oct 24, 2016, 9:05:36 PM10/24/16
to Ansible Project
So, the problem is that you can't combine lists, but you can merge dicts (it's a config setting, default=false).

This will require either a double for loop or, (my preference, especially if you are going to use this type of construct a lot) a filter to combine them.

# file: /group_vars/tors
---
vlans
:
  tor
:
 
- name: GLOBAL-VLAN
    id
: 100
    net
: "{{ globalvlan_prefix }}"
  special
: [] ## just so the template doesn't choke



# file: /group_vars/app1switches
---
vlans
:
  special
:
 
- name: SPECIAL-APP1-VLAN
    id
: 110
    net
: "{{ app1_prefix }}"



# file: /group_vars/app2switches
---
vlans
:
  special
:
 
- name: SPECIAL-APP2-VLAN
    id
: 120
    net
: "{{ app2_prefix }}"


# file: templates/switches.j2

VLANS = {{vlans|combine_dict}}



Mike Biancaniello

unread,
Oct 24, 2016, 9:26:34 PM10/24/16
to Ansible Project
example filter:  https://github.com/chepazzo/ansible-misc/blob/master/filter_plugins/listoflists.py#L35

Now, your template looks like this:

{% for vlan in vlans|collapse_dict %}
set vlan {{ vlan.name }} vlan-id {{ vlan.id }}
{% endfor %}


Reply all
Reply to author
Forward
0 new messages