I'd like to have a pattern for roles where I can:
1) set default values in defaults/default.yml
2) override certain parts of a deep data structure in defaults/{{ansible_distribution}}.yml
3) allow individual instantiations of that role to override specific parts of it.
I have a complex data structure for configuring the settings of a certain software.
A more or less complete yaml description of the data structure might be something like:
Here is a slightly odd technique that you could possibly use.
The first step would be to make your ‘role/defaults’ data structure. In my example it looks like this:
car: make: "{{ makevar }}" model: "{{ modelvar }}" year: "{{ yearvar }}" condition: paint: "{{ paintvar }}" frame: "{{ framvar }}"
Notice that I am parameterizing all the values and the default is merely functioning as the skeleton.
Now in ‘group_vars/all’ I wrote this in:
`
makevar: Ford
modelvar: Mustang
yearvar: 2009
paintvar: Great
framvar: Excellent
`
This establishes the default values for the initial data structure.
Now for the purposes of my example I made a simple role with the following task:
`
-
name: Show this value
debug: var=car.model
-
name: show this other value
debug: var=car.make
-
name: this deeper value
debug: var=car.condition.paint
`
when I run the playbook, it renders the values correctly. Now of course you could flip the group_vars/all with somerole/defaults and it would work the same, it would come down to matter of preference, your needs in terms of variable precedence and how you plan to structure the rest of your Ansible setup.
Let’s assume the role is called ‘coolrole’ if you need to override specific parts in various instantiations of the role you would do something like the following:
`