Vars question, bug or by design?

I have been using nested vars in my group variables and ran into an issue when trying to nest variables in my playbooks. I have my ansible.cfg set to merge hashes (hash_behavior=merge). This works great for group and host vars but when I put include a vars hash in my playbooks, it overwrites the whole hash with the variables in the playbook even when none of the hash keys are shared with the group/host vars. Is this expected behavior?

I dug into the code a bit and realized it would be a bit tricky to get this to work as I would expect it. Before I dig any further, I just want to make sure this is not expected behavior.

Thanks,
Chris

hash merging is pretty new in Ansible, I'd say it's reasonable to
assume the behavior for that was completely undefined, but it seems
more logical if everything merged in that case.

It might be useful to isolate the 'will-it-blend' behavior to a
function in utils that checks the config setting, rather than having
this in more than one place.

I have been using nested vars in my group variables and ran into an issue
when trying to nest variables in my playbooks. I have my ansible.cfg set to
merge hashes (hash_behavior=merge). This works great for group and host
vars but when I put include a vars hash in my playbooks, it overwrites the
whole hash with the variables in the playbook even when none of the hash
keys are shared with the group/host vars. Is this expected behavior?

hash merging is pretty new in Ansible, I'd say it's reasonable to
assume the behavior for that was completely undefined, but it seems
more logical if everything merged in that case.

I'm the guy to slap for this. Christopher, could you file a ticket ?

Yes, it defitively should merge hashes in a logical order, which is
described here
http://ansible.cc/docs/playbooks2.html?highlight=precedence#understanding-variable-precedence
(the later overrides the former). Here is how I understand things.

Please correct me when I'm wrong :

1/ Any variables specified with �extra-vars (-e) on the ansible-playbook
command line.

I don't think complex vars can be defined here (?)

2/ Variables loaded from YAML files mentioned in �vars_files� in a playbook.

I suppose complex vars can be defined here.
If this is the first source of complex vars, the merge only happens
after this step.

3/ facts, whether built in or custom, or variables assigned from the
�register� keyword.

Should merge over previous vars.

4/ variables passed to parameterized task include statements

I don't think parameters to tasks can be complex (?).

5/ �vars� as defined in the playbook.

Should merge over previous vars.

6/ Host variables from inventory.

As far as the discussion went, Michael doesn't want complex vars to be
defined at that level. However, I have a patch here, so if this sounds
reasonable in the end, I can push that. Since this would be new, it
probably can't harm existing pbooks. It seems reasonable to me to make
things behave consistently here too.

7/ Group variables from inventory in inheritance order

This is the only place where merging works for now.

Am I getting it right here ?

BTW I don't understand why 2/ and 5/ are so far away, since they are two
different ways of defining the same thing (one "modular", one "inline").

when I put include a vars hash in my playbooks, it overwrites the
whole hash with the variables in the playbook even when none of the hash
keys are shared with the group/host vars. Is this expected behavior?

You mean var hash in the playbook (5) overrides var hash defined in
group/host (7) vars ? It doesn't seem to comply with the order defined
in the docs.

It might be useful to isolate the 'will-it-blend' behavior to a
function in utils that checks the config setting, rather than having
this in more than one place.

Sure. I didn't update the docs on this either (there's only a blurb in
example/ansible.cfg) and this is definitively missing. I can take a look
at this once the issue is settled.

M

You mean var hash in the playbook (5) overrides var hash defined in
group/host (7) vars ? It doesn’t seem to comply with the order defined
in the docs.

I may have the order of which overwrites which incorrect since I have already worked around this issue. As for precedence, I think I have seen it suggested to not rely on the precedence and it not being a best practice, so I am not using it. All my hash keys are mutually exclusive between my group/host vars and playbook vars, they are just defined at a different level in my hierarchy.

I’m the guy to slap for this. Christopher, could you file a ticket ?

No slaps necessary, I will file a ticket.

Issue created https://github.com/ansible/ansible/issues/2329