I am new to Ansible and am setting up a role to generate configs for multiple devices. For this post lets use switches as the example. Let's say I want to configure switches for multiple customers. This means there will be many settings are that are similar and some that are unique. I want all the similar settings to go in defaults/main.yml and the unique settings to go vars/main.yml with ability to overwrite defaults/main.yml.
What I have so far...
tasks/main.yml
---
- name: Write config out to file
template:
src: swtiches.j2
dest: /opt/ansible-output/{{ item.customer_name }}/{{ item.hostname }}.cfg
with_items: "{{switches}}"
vars/main.yml
---
switches:
- hostname: SW1
customer_name: ACME1
mgmt_ip: 10.0.0.8
mgmt_ip_subnet_mask: 255.255.255.0
mgmt_gateway: 10.0.0.1
- hostname: SW2
customer_name: ACME2
mgmt_ip: 10.1.1.8
mgmt_ip_subnet_mask: 255.255.255.0
mgmt_gateway: 10.1.1.1
My understanding is that because I am using with_items in tasks/main.yml my variables in vars/main.yml need to be a dictionary (to iterate over multiple devices). This is where the problem comes in. How do I format things in defaults/main.yml in order to preserve variable precedence rules?
Let's say the majority of my customers are in eastern timezone so I want to put
timezone: America/New_York
in defaults/main.yml but on occasion I want to add
timezone: America/Chicago
to vars/main.yml to override the default.
NOTE1: If I don't use with_items in tasks/main.yml so that I only configure one device and I use key:value pairs in both defaults/main.yml and vars/main.yml I can get to override to work. This shows the concept of overriding works....just not when I add the complexity of multiple devices.
NOTE2: If I setup my variables as a dictionary in both main.yml files and use the combine function (which I was given in a previous post) I can combine and use variables from both defaults/main.yml and vars/main.yml BUT....I lose the ability to override variables given the nature of combining dictionaries.
QUESTIONS: Do precedence rules only work when using key:value pairs?
If not, how do I set up defaults/main.yml to work using with_items and preserve the ability to overwrite using the various precedence rules (group_vars, host_vars, extra_vars, etc)?