How to override nested variables?

2,439 views
Skip to first unread message

Eric Chaves

unread,
Dec 1, 2013, 7:04:15 PM12/1/13
to ansible...@googlegroups.com
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:
---
  mysql:
      address: 127.0.0.1
      admin: admin
      password: 12345

Then in the my_server.yml:
---
- hosts: myserver
- roles:
  - mysql
- vars:
    mysql:
       password: abcdef

but once I do this mysql.address and mysql.admin become undefined. I have tried the mysql[address] syntax but it didn't work.

Is it possible to override just a few set of nested attributes?

Thanks for the help,

Eric

Kahlil Hodgson

unread,
Dec 1, 2013, 7:22:47 PM12/1/13
to ansible...@googlegroups.com
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").

---
- hosts: myserver
- roles:
- mysql
- vars:
mysql_password: abcdef
- tasks:
- debug: msg="{{ mysql_password | default(mysql.password) }}"

This way it is clear that the password could have come from two
different places.

Hope this helps.

K


Kahlil (Kal) Hodgson GPG: C9A02289
Head of Technology (m) +61 (0) 4 2573 0382
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer." -- IBM maintenance manual, 1925
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Eric Chaves

unread,
Dec 1, 2013, 8:07:06 PM12/1/13
to ansible...@googlegroups.com
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,


--
Eric Chaves
(11) 98139-9880


2013/12/1 Kahlil Hodgson <kahlil....@dealmax.com.au>
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/klSyFJxtLqg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

Michael DeHaan

unread,
Dec 1, 2013, 8:13:07 PM12/1/13
to ansible...@googlegroups.com
"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 DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Eric Chaves

unread,
Dec 1, 2013, 8:32:04 PM12/1/13
to ansible...@googlegroups.com
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.

--
Eric Chaves
(11) 98139-9880


2013/12/1 Michael DeHaan <mic...@ansibleworks.com>

Serge van Ginderachter

unread,
Dec 2, 2013, 2:50:28 AM12/2/13
to ansible...@googlegroups.com

On 2 December 2013 02:32, Eric Chaves <er...@craftti.com.br> wrote:
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?

​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

Michael DeHaan

unread,
Dec 2, 2013, 9:00:39 AM12/2/13
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Eric Chaves

unread,
Dec 4, 2013, 6:23:18 AM12/4/13
to ansible...@googlegroups.com
Great! Thanks all for the inputs and help!


--
Eric Chaves
(11) 98139-9880


2013/12/2 Michael DeHaan <mic...@ansibleworks.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/klSyFJxtLqg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages