0

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)?

pizzaguy39
  • 87
  • 5
  • So `timezone` might sometimes be part of your list under `switch` (make yourself a favour, and call that dictionary `switches`, rather, so you know there are many there)? And what is `item.customer_name`, this is not part of your dictionaries in your list. – β.εηοιτ.βε Mar 08 '22 at 20:27
  • 1
    You can try and set [DEFAULT_HASH_BEHAVIOUR](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-hash-behaviour) to ``merge``. Minimize and simplify your example as much as possible. [edit] the question and make it [mre]. Remove all roles, loops, complex paths, templates, etc, and test the simple dictionaries only by debug. Once you understand how the dictionaries are being merged move on. – Vladimir Botka Mar 09 '22 at 00:30
  • 1
    It's important to understand how the variables are evaluated. Quoting from [Glossary](https://docs.ansible.com/ansible/latest/reference_appendices/glossary.html#glossary) **Lazy Evaluation**: *'In general, Ansible evaluates any variables in playbook content at the last possible second ...'* – Vladimir Botka Mar 09 '22 at 00:36
  • I cleaned up my question slightly. I don't think I can boil it down much further. In a nut shell I am using a role where I want to iterate over `with_items` and use the concept of precedence rules to store 90% of my variables under `defaults` folder and the other 10% under `vars` folder. What does the syntax in `main.yml` look like in both of those folders? Are dictionaries my only option? I could be wrong but I don't feel I am inventing the wheel here....this seems it would be a common use case which should have a simple solution. – pizzaguy39 Mar 09 '22 at 17:05
  • @pizzaguy39 It is still unclear to me if you want the precedence to work over items of lists (so what is under `switches`) or in other variables. And how is time `timezone` related to that `switch` variable. Could you provide an expected result of what this loop should give and what is its relation with `timezone`? – β.εηοιτ.βε Mar 13 '22 at 20:24

0 Answers0