Error when combining variables

827 views
Skip to first unread message

Jiri

unread,
Feb 24, 2015, 8:21:19 AM2/24/15
to ansible...@googlegroups.com
I'm getting the following error:

ERROR: failed to combine variables, expected dicts but got a 'dict' and a 'str'

with the following setup:

# ./site.yaml
---

- hosts: all
  connection: local
  gather_facts: no
  vars:
    xxx:
      aaa: 1
  roles:
    - role1

# ./roles/role1/vars/main.yaml
---

xxx: "{{ my_xxx }}"

# ./roles/role1/defaults/main.yaml
---

my_xxx:
  bbb: 2

# ./roles/role1/tasks/main.yaml
---

- debug:
    msg: xxx={{ xxx }}

# ./hosts
localhost

There is no error if there is no vars definition in the site.yaml (no hashes to be 'combined'). The problem is that the value of xxx coming from the role is still Jinja2 template string ("{{ my_xxx }}" - which is in fact a string, not a hash) when the merge happens. Interesting is that if I turn the hash ("bbb: 2", "aaa: 1") to a list ("- bbb: 2", "- aaa: 1") all works as expected.

The question is why it's even trying to merge the two variables? I would expect the variable to be replaced and not merged as per default setting hash_behaviour=replace in ansible.cfg.

Any comments which will help me to understand this behavior are more than appreciated.

Jiri

unread,
Feb 24, 2015, 9:30:59 AM2/24/15
to ansible...@googlegroups.com
I have created the following pull request to fix this issue:

Serge van Ginderachter

unread,
Feb 24, 2015, 9:53:54 AM2/24/15
to ansible...@googlegroups.com

On 24 February 2015 at 14:21, Jiri <jiri...@gmail.com> wrote:
The question is why it's even trying to merge the two variables? I would expect the variable to be replaced and not merged as per default setting hash_behaviour=replace in ansible.cfg.


​That reason seems to be, merging happens on higher-level internal variables (setup cache, various ​play vars, inventory vars, etc.)
The merge_hash then happens recursively, which is why you see that error, even if you don;t have the merge setting.

I'm not sure your proposed patch solves the deeper issue, only what happens in your case, and might potentially break other things.

The issue here is {{ my_xxx }} being cast to a string instead of a dictionary.


Jiri

unread,
Feb 24, 2015, 10:07:13 AM2/24/15
to ansible...@googlegroups.com
Thanks for your comments, Serge.

On Tuesday, February 24, 2015 at 2:53:54 PM UTC, Serge van Ginderachter wrote:
I'm not sure your proposed patch solves the deeper issue, only what happens in your case, and might potentially break other things.

It depends for what the function is used. If it's used to merge variables on the same level (vars_files, vars, ...), then it should not break anything.

The issue here is {{ my_xxx }} being cast to a string instead of a dictionary.

Yes, that's exactly the problem. It's interesting that in the case of lists it works just fine. One would expect that the same concept could be used for lists as well as for dicts.

Brian Coca

unread,
Feb 24, 2015, 10:19:45 AM2/24/15
to ansible...@googlegroups.com
I am currently testing, https://github.com/ansible/ansible/pull/10329
<= attempt to correct the deeper issue, this is has been in 1.8 since
a re-factoring on variable merging started using the wrong function.

Any help testing this PR will be appreciated, this can have far
reaching consequences.

--
Brian Coca
Reply all
Reply to author
Forward
0 new messages