Last active
September 15, 2017 07:31
-
-
Save halberom/093d9958adf87a8df880 to your computer and use it in GitHub Desktop.
ansible - jinja filter for dict merging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from jinja2.utils import soft_unicode | |
def merge_dicts(value, dict1): | |
# return a merged dict | |
result = {} | |
result.update(value) | |
result.update(dict1) | |
return result | |
class FilterModule(object): | |
''' Ansible jinja2 filters ''' | |
def filters(self): | |
return { | |
'merge_dicts': merge_dicts, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TASK: [debug var=foo] ********************************************************* | |
ok: [centos66] => { | |
"var": { | |
"foo": { | |
"bar": "a string", | |
"fox": "jumpped", | |
"over": "lazy dog" | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: centos66 | |
remote_user: vagrant | |
gather_facts: no | |
vars: | |
foo: | |
bar: 'a string' | |
tasks: | |
- set_fact: | |
foo: "{{ foo | merge_dicts(item) }}" | |
with_items: | |
- { 'fox': 'jumpped', 'over': 'lazy dog' } | |
- debug: var=foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment