mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Added basic support for hash_behaviour=merge in roles
Dict vars passed to roles are now properly merged instead of simply overriding dict vars that are coming from vars_files.
This commit is contained in:
parent
fc2d25eb82
commit
c642ba77ae
6 changed files with 74 additions and 53 deletions
|
@ -269,7 +269,7 @@ class Play(object):
|
||||||
raise errors.AnsibleError("parse error: task includes cannot be used with other directives: %s" % k)
|
raise errors.AnsibleError("parse error: task includes cannot be used with other directives: %s" % k)
|
||||||
|
|
||||||
if 'vars' in x:
|
if 'vars' in x:
|
||||||
task_vars.update(x['vars'])
|
task_vars = utils.combine_vars(task_vars, x['vars'])
|
||||||
if 'only_if' in x:
|
if 'only_if' in x:
|
||||||
included_additional_conditions.append(x['only_if'])
|
included_additional_conditions.append(x['only_if'])
|
||||||
|
|
||||||
|
|
|
@ -335,24 +335,23 @@ def parse_kv(args):
|
||||||
return options
|
return options
|
||||||
|
|
||||||
def merge_hash(a, b):
|
def merge_hash(a, b):
|
||||||
''' merges hash b into a
|
''' recursively merges hash b into a
|
||||||
this means that if b has key k, the resulting has will have a key k
|
keys from b take precedende over keys from a '''
|
||||||
which value comes from b
|
|
||||||
said differently, all key/value combination from b will override a's '''
|
|
||||||
|
|
||||||
# and iterate over b keys
|
result = copy.deepcopy(a)
|
||||||
|
|
||||||
|
# next, iterate over b keys and values
|
||||||
for k, v in b.iteritems():
|
for k, v in b.iteritems():
|
||||||
if k in a and isinstance(a[k], dict):
|
# if there's already such key in a
|
||||||
# if this key is a hash and exists in a
|
# and that key contains dict
|
||||||
# we recursively call ourselves with
|
if k in result and isinstance(result[k], dict):
|
||||||
# the key value of b
|
# merge those dicts recursively
|
||||||
a[k] = merge_hash(a[k], v)
|
result[k] = merge_hash(a[k], v)
|
||||||
else:
|
else:
|
||||||
# k is not in a, no need to merge b, we just deecopy
|
# otherwise, just copy a value from b to a
|
||||||
# or k is not a dictionnary, no need to merge b either, we just deecopy it
|
result[k] = v
|
||||||
a[k] = v
|
|
||||||
# finally, return the resulting hash when we're done iterating keys
|
return result
|
||||||
return a
|
|
||||||
|
|
||||||
def md5s(data):
|
def md5s(data):
|
||||||
''' Return MD5 hex digest of data. '''
|
''' Return MD5 hex digest of data. '''
|
||||||
|
|
|
@ -96,6 +96,8 @@ class TestPlaybook(unittest.TestCase):
|
||||||
os.unlink('/tmp/ansible_test_data_template.out')
|
os.unlink('/tmp/ansible_test_data_template.out')
|
||||||
if os.path.exists('/tmp/ansible_test_messages.out'):
|
if os.path.exists('/tmp/ansible_test_messages.out'):
|
||||||
os.unlink('/tmp/ansible_test_messages.out')
|
os.unlink('/tmp/ansible_test_messages.out')
|
||||||
|
if os.path.exists('/tmp/ansible_test_role_messages.out'):
|
||||||
|
os.unlink('/tmp/ansible_test_role_messages.out')
|
||||||
|
|
||||||
def _prepare_stage_dir(self):
|
def _prepare_stage_dir(self):
|
||||||
stage_path = os.path.join(self.test_dir, 'test_data')
|
stage_path = os.path.join(self.test_dir, 'test_data')
|
||||||
|
@ -304,20 +306,17 @@ class TestPlaybook(unittest.TestCase):
|
||||||
)
|
)
|
||||||
playbook.run()
|
playbook.run()
|
||||||
|
|
||||||
with open('/tmp/ansible_test_messages.out') as f:
|
filename = '/tmp/ansible_test_messages.out'
|
||||||
actual = [l.strip() for l in f.readlines()]
|
expected_lines = [
|
||||||
|
|
||||||
print "**ACTUAL**"
|
|
||||||
print actual
|
|
||||||
|
|
||||||
expected = [
|
|
||||||
"goodbye: Goodbye World!"
|
"goodbye: Goodbye World!"
|
||||||
]
|
]
|
||||||
|
self._compare_file_output(filename, expected_lines)
|
||||||
|
|
||||||
print "**EXPECTED**"
|
filename = '/tmp/ansible_test_role_messages.out'
|
||||||
print expected
|
expected_lines = [
|
||||||
|
"inside_a_role: Indeed!"
|
||||||
assert actual == expected
|
]
|
||||||
|
self._compare_file_output(filename, expected_lines)
|
||||||
|
|
||||||
# restore default hash behavior
|
# restore default hash behavior
|
||||||
C.DEFAULT_HASH_BEHAVIOUR = saved_hash_behavior
|
C.DEFAULT_HASH_BEHAVIOUR = saved_hash_behavior
|
||||||
|
@ -337,21 +336,34 @@ class TestPlaybook(unittest.TestCase):
|
||||||
)
|
)
|
||||||
playbook.run()
|
playbook.run()
|
||||||
|
|
||||||
with open('/tmp/ansible_test_messages.out') as f:
|
filename = '/tmp/ansible_test_messages.out'
|
||||||
actual = [l.strip() for l in f.readlines()]
|
expected_lines = [
|
||||||
|
"goodbye: Goodbye World!",
|
||||||
print "**ACTUAL**"
|
"hello: Hello World!"
|
||||||
print actual
|
|
||||||
|
|
||||||
expected = [
|
|
||||||
"hello: Hello World!",
|
|
||||||
"goodbye: Goodbye World!"
|
|
||||||
]
|
]
|
||||||
|
self._compare_file_output(filename, expected_lines)
|
||||||
|
|
||||||
print "**EXPECTED**"
|
filename = '/tmp/ansible_test_role_messages.out'
|
||||||
print expected
|
expected_lines = [
|
||||||
|
"goodbye: Goodbye World!",
|
||||||
assert actual == expected
|
"hello: Hello World!",
|
||||||
|
"inside_a_role: Indeed!"
|
||||||
|
]
|
||||||
|
self._compare_file_output(filename, expected_lines)
|
||||||
|
|
||||||
# restore default hash behavior
|
# restore default hash behavior
|
||||||
C.DEFAULT_HASH_BEHAVIOUR = saved_hash_behavior
|
C.DEFAULT_HASH_BEHAVIOUR = saved_hash_behavior
|
||||||
|
|
||||||
|
def _compare_file_output(self, filename, expected_lines):
|
||||||
|
actual_lines = []
|
||||||
|
with open(filename) as f:
|
||||||
|
actual_lines = [l.strip() for l in f.readlines()]
|
||||||
|
actual_lines = sorted(actual_lines)
|
||||||
|
|
||||||
|
print "**ACTUAL**"
|
||||||
|
print actual_lines
|
||||||
|
|
||||||
|
print "**EXPECTED**"
|
||||||
|
print expected_lines
|
||||||
|
|
||||||
|
assert actual_lines == expected_lines
|
||||||
|
|
|
@ -9,3 +9,8 @@
|
||||||
tasks:
|
tasks:
|
||||||
- name: generate messages
|
- name: generate messages
|
||||||
action: template src=message.j2 dest=/tmp/ansible_test_messages.out
|
action: template src=message.j2 dest=/tmp/ansible_test_messages.out
|
||||||
|
|
||||||
|
roles:
|
||||||
|
- role: hash_behavior_test_role
|
||||||
|
messages:
|
||||||
|
inside_a_role: "Indeed!"
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
- name: generate role messages
|
||||||
|
action: template src=role_message.j2 dest=/tmp/ansible_test_role_messages.out
|
|
@ -0,0 +1,3 @@
|
||||||
|
{% for k, v in messages.iteritems() %}
|
||||||
|
{{ k }}: {{ v }}
|
||||||
|
{% endfor %}
|
Loading…
Reference in a new issue