vars merging

2,497 views
Skip to first unread message

rektide

unread,
Mar 25, 2013, 12:24:02 AM3/25/13
to ansible...@googlegroups.com
Hi,

I'd like to have a default vars files with a number of complex YAML objects, and then allow
users to come in and define their own YAML files that are merged on top of the existing YAML
objects. At present Ansible behavior is that the top level object is overriden- is there any
capability or technique I can adopt to use one vars to supplement an existing vars?

first.vars:
---
main:
hi:
one: 1
two: 2

second.vars:
---
main:
hi:
two: 777
three: 3
four: 4

desired final state:
{main:{hi:{one:1,two:777,three:3,four:4}}}

Please let me know if this problem statement is unclear.

Thanks,
rektide

Serge van Ginderachter

unread,
Mar 25, 2013, 2:39:08 AM3/25/13
to ansible...@googlegroups.com

On 25 March 2013 05:24, rektide <rek...@voodoowarez.com> wrote:
I'd like to have a default vars files with a number of complex YAML objects, and then allow
users to come in and define their own YAML files that are merged on top of the existing YAML
objects. At present Ansible behavior is that the top level object is overriden- is there any
capability or technique I can adopt to use one vars to supplement an existing vars?

ATM this is AFAIK not possible, but

+1 for me, I have some use cases where this would be nice to have.


Serge

C. Morgan Hamill

unread,
Mar 25, 2013, 3:00:17 AM3/25/13
to ansible...@googlegroups.com
In ansible.cfg, under the '[defaults]' section, you can put 'hash_behavior = merge'. I think this is new as of the pending 1.1 release, so you'll need a gift checkout.

See <https://github.com/ansible/ansible/blob/devel/examples/ansible.cfg> for an example config file.
--
Morgan Hamill

C. Morgan Hamill

unread,
Mar 25, 2013, 2:29:47 PM3/25/13
to ansible-project
Excerpts from seb.stark's message of 2013-03-25 10:17:18 -0400:
> Thanks for noting! Is this also possible for lists?

I don't believe so.
--
Morgan

Michael DeHaan

unread,
Mar 25, 2013, 2:56:09 PM3/25/13
to ansible...@googlegroups.com
Nope, there's no good way to tell what behavior you would want with lists.
> --
> 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.
>
>



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

seb....@gmail.com

unread,
Mar 26, 2013, 3:44:40 AM3/26/13
to ansible...@googlegroups.com
My use case is a packages variable in group_vars/all that I want to append to in group_vars/<group> and host_vars/<host>. So far I use two more variables (group_packages and host_packages), that default to [] and separate actions for using them.

But maybe there is already a better way to do this?

Michael DeHaan

unread,
Mar 26, 2013, 8:49:27 AM3/26/13
to ansible...@googlegroups.com
Your approach would work, though if hosts were in more than one group,
that doesn't work so much.

Generally I find the purpose of plays though to bind hosts to roles,
so the individual roles should contain the package lists.

I would not try to store everything in variables that way.

In fact, you should almost never have host specific variables related
to behavior -- networking and hardware configurations yes, but very
seldom behavior.





On Tue, Mar 26, 2013 at 3:44 AM, <seb....@gmail.com> wrote:
> My use case is a packages variable in group_vars/all that I want to append to in group_vars/<group> and host_vars/<host>. So far I use two more variables (group_packages and host_packages), that default to [] and separate actions for using them.
>
> But maybe there is already a better way to do this?
>

seb....@gmail.com

unread,
Mar 26, 2013, 12:01:53 PM3/26/13
to ansible...@googlegroups.com

Good point. I already noticed my system isn't working well because I have roles that add yet more packages to individual hosts. In fact, those explicit package lists are the only contents of my host_vars/* files and I should really get rid of them.  Thanks for the heads up!
Reply all
Reply to author
Forward
0 new messages