Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include_vars task does not merge hashes if told so via ansible config #9116

Closed
udondan opened this issue Sep 24, 2014 · 10 comments
Closed

include_vars task does not merge hashes if told so via ansible config #9116

udondan opened this issue Sep 24, 2014 · 10 comments

Comments

@udondan
Copy link

udondan commented Sep 24, 2014

Issue Type:

Bug Report

Ansible Version:
$ ansible --version
ansible 1.7.1
Environment:

Local: OS X Mavericks
Remote: Debian

Summary:

include_vars task does not merge hashes if told so via ansible config.

Steps To Reproduce:

ansible.cfg contains this line:

hash_behaviour = merge

Playbook with 3 tasks:

- include_vars: /path/to/file/A

- include_vars: /path/to/file/B

- debug: var=test

Content of file A:

test:
  a: 1

Content of file B:

test:
  b: 1
Expected Results:

The merged hash test should contain both keys, a and b.

TASK: [include_vars /path/to/file/A] *** 
ok: [test1.local]


TASK: [include_vars /path/to/file/B] *** 
ok: [test1.local]


TASK: [debug var=test] ****************************************** 
ok: [test1.local] => {
    "test": {
        "a": 1,
        "b": 1
    }
}
Actual Results:

The hash keys have not been merged. Only the latter one, b is available.

TASK: [include_vars /path/to/file/A] *** 
ok: [test1.local]


TASK: [include_vars /path/to/file/B] *** 
ok: [test1.local]


TASK: [debug var=test] ****************************************** 
ok: [test1.local] => {
    "test": {
        "b": 1
    }
}
@udondan
Copy link
Author

udondan commented Sep 24, 2014

BTW, where is the code of that component? The only file I was able to find only contains documentation, not the actual code. https://github.com/ansible/ansible/blob/75e5b6401c65da9de8ec4acee0dd54cabdcdb546/library/utilities/include_vars

@srgvg
Copy link
Contributor

srgvg commented Sep 24, 2014

Part of the code is in https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/action_plugins/include_vars.py

The merging however happens somewhere in the runner I believe.

Those vars are actually returned as ansible_facts, so merging include_vars would mean merging ansible_facts AFAICS.

@udondan
Copy link
Author

udondan commented Sep 24, 2014

In that case this closed PR looks like it could have been the solution? #7700

@srgvg
Copy link
Contributor

srgvg commented Sep 24, 2014

Yes, that looks like it..

@srgvg
Copy link
Contributor

srgvg commented Sep 24, 2014

Given Michaels comments on #7700:

FWIW and AFAICS: choosing wether to merge or not is done separately for
vars returned from:

  • module returning ansible_facts key (which is the use case for
    include_vars)
  • set_fact module
  • the setup module

@udondan
Copy link
Author

udondan commented Sep 25, 2014

If anyone has the same problem, I created an action plugin include_vars_merged which will do just that. Additionally it will merge hashes no matter how you have set up your hash_behaviour.

https://gist.github.com/udondan/b730206056a018cf9838

@jimi-c
Copy link
Member

jimi-c commented Sep 26, 2014

This could be done apart from ansible_facts, by making the result parser look for something beyond ansible_facts, ie. ansible_vars. Those could then be merged into the vars_cache (as opposed to the setup_cache), which is probably more appropriate anyway without having to worry about merging facts from runs.

The relevant code is in lib/ansible/playbook/__init__.py, around line 515, if you'd like to look into that approach.

Based on the above discussion though, and that on #7700, I'll go ahead and close this. If you have any further questions, please let us know by stopping by one of the two mailing lists, as appropriate:

Because this project is very active, we're unlikely to see comments made on closed tickets, but the mailing list is a great way to ask questions, or post if you don't think this particular issue is resolved.

Thank you!

@jimi-c jimi-c closed this as completed Sep 26, 2014
@udondan
Copy link
Author

udondan commented Sep 26, 2014

If you look into my gist you will see that this can never be solved inside the runner. Merging needs to happen in the plugin. Anyway, I expected nothing else than this to be closed, therefore solved myself with the plugin.

@awasilyev
Copy link
Contributor

@udondan, don't you update your code for ansible-2.0?

@mprinc
Copy link

mprinc commented Oct 15, 2018

Starting with Ansible v2.0 you can do it:

  • name: merging hash_a and hash_b into hash_c
    set_fact: hash_c="{{ hash_a|combine(hash_b) }}"
    Check more under Ansible filters - Combining hashes/dictionaries (coming from Jinja2)

@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants