How to override nested variables?

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:

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").

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,

“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.

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.

​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

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.

Great! Thanks all for the inputs and help!