Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active September 15, 2017 07:31
Show Gist options
  • Save halberom/093d9958adf87a8df880 to your computer and use it in GitHub Desktop.
Save halberom/093d9958adf87a8df880 to your computer and use it in GitHub Desktop.
ansible - jinja filter for dict merging
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,
}
TASK: [debug var=foo] *********************************************************
ok: [centos66] => {
"var": {
"foo": {
"bar": "a string",
"fox": "jumpped",
"over": "lazy dog"
}
}
}
---
- 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