# Variable inheritance/override

**URL:** <https://forum.ansible.com/t/variable-inheritance-override/17730>\
**Category:** Ansible Project\
**Created:** [March 12, 2015, 5:06pm UTC](https://forum.ansible.com/t/variable-inheritance-override/17730 "2015-03-12T17:06:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duncan\_Hutty](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@Duncan\_Hutty](https://forum.ansible.com/u/Duncan_Hutty)\
**Post date:** [March 12, 2015, 5:06pm UTC](https://forum.ansible.com/t/variable-inheritance-override/17730/1 "2015-03-12T17:06:05Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Jonathan\_Davila](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@Jonathan\_Davila](https://forum.ansible.com/u/Jonathan_Davila)\
**Post date:** [March 13, 2015, 2:13pm UTC](https://forum.ansible.com/t/variable-inheritance-override/17730/2 "2015-03-13T14:13:31Z")

</div>

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:

`
