Last active
August 29, 2015 14:16
-
-
Save odyssey4me/bb1ae8d6f8b6c00a730a to your computer and use it in GitHub Desktop.
ansible-dict-merge
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
[defaults] | |
jinja2_extensions=jinja2.ext.do |
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
[all] | |
localhost ansible_connection=local |
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
PLAY [test play for dict merge] *********************************************** | |
GATHERING FACTS *************************************************************** | |
<127.0.0.1> REMOTE_MODULE setup | |
ok: [127.0.0.1] | |
TASK: [output default values] ************************************************* | |
ok: [127.0.0.1] => (item={'key': 'section2', 'value': {'s2key1': 'default_value3'}}) => { | |
"item": { | |
"key": "section2", | |
"value": { | |
"s2key1": "default_value3" | |
} | |
}, | |
"msg": "section2 = {'s2key1': 'default_value3'}" | |
} | |
ok: [127.0.0.1] => (item={'key': 'section1', 'value': {'s1key1': 'default_value1', 's1key2': 'default_value2'}}) => { | |
"item": { | |
"key": "section1", | |
"value": { | |
"s1key1": "default_value1", | |
"s1key2": "default_value2" | |
} | |
}, | |
"msg": "section1 = {'s1key1': 'default_value1', 's1key2': 'default_value2'}" | |
} | |
TASK: [output tunable values] ************************************************* | |
ok: [127.0.0.1] => (item={'key': 'section3', 'value': {'s3key1': 'tunable_value3'}}) => { | |
"item": { | |
"key": "section3", | |
"value": { | |
"s3key1": "tunable_value3" | |
} | |
}, | |
"msg": "section3 = {'s3key1': 'tunable_value3'}" | |
} | |
ok: [127.0.0.1] => (item={'key': 'section1', 'value': {'s1key3': 'tunable_value2', 's1key2': 'tunable_value1'}}) => { | |
"item": { | |
"key": "section1", | |
"value": { | |
"s1key2": "tunable_value1", | |
"s1key3": "tunable_value2" | |
} | |
}, | |
"msg": "section1 = {'s1key3': 'tunable_value2', 's1key2': 'tunable_value1'}" | |
} | |
TASK: [merge default and tunable values] ************************************** | |
ok: [127.0.0.1] => {"ansible_facts": {"merged_dict": {"section1": {"s1key1": "default_value1", "s1key2": "tunable_value1", "s1key3": "tunable_value2"}, "section2": {"s2key1": "default_value3"}, "section3": {"s3key1": "tunable_value3"}}}} | |
TASK: [output merged values] ************************************************** | |
ok: [127.0.0.1] => (item={'key': 'section3', 'value': {'s3key1': 'tunable_value3'}}) => { | |
"item": { | |
"key": "section3", | |
"value": { | |
"s3key1": "tunable_value3" | |
} | |
}, | |
"msg": "section3 = {'s3key1': 'tunable_value3'}" | |
} | |
ok: [127.0.0.1] => (item={'key': 'section2', 'value': {'s2key1': 'default_value3'}}) => { | |
"item": { | |
"key": "section2", | |
"value": { | |
"s2key1": "default_value3" | |
} | |
}, | |
"msg": "section2 = {'s2key1': 'default_value3'}" | |
} | |
ok: [127.0.0.1] => (item={'key': 'section1', 'value': {'s1key1': 'default_value1', 's1key3': 'tunable_value2', 's1key2': 'tunable_value1'}}) => { | |
"item": { | |
"key": "section1", | |
"value": { | |
"s1key1": "default_value1", | |
"s1key2": "tunable_value1", | |
"s1key3": "tunable_value2" | |
} | |
}, | |
"msg": "section1 = {'s1key1': 'default_value1', 's1key3': 'tunable_value2', 's1key2': 'tunable_value1'}" | |
} | |
PLAY RECAP ******************************************************************** | |
127.0.0.1 : ok=5 changed=0 unreachable=0 failed=0 |
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
- name: test play for dict merge | |
hosts: 127.0.0.1 | |
connection: local | |
vars: | |
default_dict: | |
section1: | |
s1key1: "default_value1" | |
s1key2: "default_value2" | |
section2: | |
s2key1: "default_value3" | |
tunable_dict: | |
section1: | |
s1key2: "tunable_value1" | |
s1key3: "tunable_value2" | |
section3: | |
s3key1: "tunable_value3" | |
tasks: | |
- name: output default values | |
debug: msg="{{ item.key }} = {{ item.value }}" | |
with_dict: default_dict | |
- name: output tunable values | |
debug: msg="{{ item.key }} = {{ item.value }}" | |
with_dict: tunable_dict | |
- name: merge default and tunable values | |
set_fact: | |
merged_dict: "{% for key, value in tunable_dict.iteritems() %}{% if default_dict[key] is defined %}{% do default_dict[key].update(tunable_dict[key]) %}{% else %}{% do default_dict.update({key:value}) %}{% endif %}{% endfor %}{{ default_dict }}" | |
- name: output merged values | |
debug: msg="{{ item.key }} = {{ item.value }}" | |
with_dict: merged_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment