# How to override nested variables?

**URL:** <https://forum.ansible.com/t/how-to-override-nested-variables/14040>\
**Category:** Ansible Project\
**Created:** [December 2, 2013, 12:04am UTC](https://forum.ansible.com/t/how-to-override-nested-variables/14040 "2013-12-02T00:04:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric2](https://avatars.discourse-cdn.com/v4/letter/e/8c91f0/32.png) [@eric2](https://forum.ansible.com/u/eric2)\
**Post date:** [December 2, 2013, 12:04am UTC](https://forum.ansible.com/t/how-to-override-nested-variables/14040/1 "2013-12-02T00:04:15Z")

</div>

Hi folks,

I created a role with default variables using a nested syntax and not I’d like to override just a few of those attributes in the playbook, but whenever I do it I end up redefining the entire variable na loosing the defaults for the non overrided values.

For example, in role/mysql/defaults/main.yml:

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [December 2, 2013, 12:22am UTC](https://forum.ansible.com/t/how-to-override-nested-variables/14040/2 "2013-12-02T00:22:47Z")

</div>

There is a variable merging setting, but I prefer being explicit about  
having a complex variable, parts of which can be overridden (rather  
than magically "doing the right thing").

---

<div class="post-metadata">

**Author:** ![eric2](https://avatars.discourse-cdn.com/v4/letter/e/8c91f0/32.png) [@eric2](https://forum.ansible.com/u/eric2)\
**Post date:** [December 2, 2013, 1:07am UTC](https://forum.ansible.com/t/how-to-override-nested-variables/14040/3 "2013-12-02T01:07:06Z")

</div>

Hi Kahil,

Thanks for the suggestion, but the problem remains. You haven’t override the default value, but instead declared a second flat variable. The problem with this approach is that if do this for every paramet I’ll double the number of used variables. Also I will have to “flatten” them all which in practice would be easier to just flatten the defaults.

But I do wish to have the nested syntax because some of my variables become very confusing when flattening and I also found it better for organization and clarity in the yml.

Still hoping to find a way to do it.

Cheers,

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 2, 2013, 1:13am UTC](https://forum.ansible.com/t/how-to-override-nested-variables/14040/4 "2013-12-02T01:13:07Z")

</div>

“I created a role with default variables using a nested syntax and not I’d like to override just a few of those attributes in the playbook”

There is no problem in doing this.

You can use parameterized roles, include a variable file, use set\_fact, or all sorts of things.

Now what you are asking is something different – you should change the hash merge behavior in ansible.cfg.

---

<div class="post-metadata">

**Author:** ![eric2](https://avatars.discourse-cdn.com/v4/letter/e/8c91f0/32.png) [@eric2](https://forum.ansible.com/u/eric2)\
**Post date:** [December 2, 2013, 1:32am UTC](https://forum.ansible.com/t/how-to-override-nested-variables/14040/5 "2013-12-02T01:32:04Z")

</div>

Michael, after reading the ansible.cfg documentation page, I found the parameter you mentioned, together with an warning against it. Since I value the experience from everyone here more than mine, would you care to explain why merging the hashes could be such bad idea?

Thanks again.

ps.: not trying to raise a flame, just like to hear about it.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [December 2, 2013, 7:50am UTC](https://forum.ansible.com/t/how-to-override-nested-variables/14040/6 "2013-12-02T07:50:28Z")

</div>

​I'm not sure which warning that is, but I can confirm you I have been  
using the merge functionality for a long time, and do not experience any  
problems with it.

Of course you have to remember or take into account where you define  
(different parts of) those hashes, ​and understand the possible  
implications.

Serge

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 2, 2013, 2:00pm UTC](https://forum.ansible.com/t/how-to-override-nested-variables/14040/7 "2013-12-02T14:00:39Z")

</div>

The config documentation was suggesting that most people leave it in the standard mode to make sure everybody’s writing playbooks the same way.

That was it.

---

<div class="post-metadata">

**Author:** ![eric2](https://avatars.discourse-cdn.com/v4/letter/e/8c91f0/32.png) [@eric2](https://forum.ansible.com/u/eric2)\
**Post date:** [December 4, 2013, 11:23am UTC](https://forum.ansible.com/t/how-to-override-nested-variables/14040/8 "2013-12-04T11:23:18Z")

</div>

Great! Thanks all for the inputs and help!
