Created
April 19, 2016 09:11
-
-
Save Sjd-Risca/ffc73afee462944629d6ec6337ba472e to your computer and use it in GitHub Desktop.
For Saltstack with jinja rendering: deep merge a dictionary into a first one
This file contains hidden or 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
{%- macro deep_merge(a, b) %} | |
{%- for k,v in b.iteritems() %} | |
{%- if v is string or v is number %} | |
{%- do a.update({ k: v }) %} | |
{%- elif v is not mapping %} | |
{%- if a[k] is not defined %} | |
{%- do a.update({ k: v }) %} | |
{%- elif a[k] is iterable and a[k] is not mapping and a[k] is not string %} | |
{%- do a.update({ k: v|list + a[k]|list}) %} | |
{%- else %} | |
{%- do a.update({ k: v }) %} | |
{%- endif %} | |
{%- elif v is mapping %} | |
{%- if a[k] is not defined %} | |
{%- do a.update({ k: v }) %} | |
{%- elif a[k] is not mapping %} | |
{%- do a.update({ k: v }) %} | |
{%- else %} | |
{%- do deep_merge(a[k], v) %} | |
{%- endif %} | |
{%- else %} | |
{%- do a.update({ k: 'ERROR: case not contempled in merging!' }) %} | |
{%- endif %} | |
{%- endfor %} | |
{%- endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment