diff options
author | Brian Coca <bcoca@ansible.com> | 2016-02-20 12:39:23 -0500 |
---|---|---|
committer | Brian Coca <bcoca@ansible.com> | 2016-02-20 12:39:23 -0500 |
commit | 1e0cf69b1c554ac071ce8fb54ca910a202186baf (patch) | |
tree | 8e7b74f7fe898f6b6f1d97ce0450d735e69982af | |
parent | 92f387f681b312169cb47d83fb6e084a0b2ae35d (diff) | |
parent | a935d61489a49d64addac2dfb53726d2d30abe5e (diff) | |
download | ansible-1e0cf69b1c554ac071ce8fb54ca910a202186baf.tar.gz |
Merge pull request #14559 from dagwieers/merge_hash
Improve efficiency of merge_hash (Ansible v1.9)
-rw-r--r-- | lib/ansible/utils/__init__.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 5670840a56..0c06df2f8e 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -813,23 +813,27 @@ def merge_hash(a, b): ''' recursively merges hash b into a keys from b take precedence over keys from a ''' - result = {} - # we check here as well as in combine_vars() since this # function can work recursively with nested dicts _validate_both_dicts(a, b) - for dicts in a, b: - # next, iterate over b keys and values - for k, v in dicts.iteritems(): - # if there's already such key in a - # and that key contains dict - if k in result and isinstance(result[k], dict): - # merge those dicts recursively - result[k] = merge_hash(a[k], v) - else: - # otherwise, just copy a value from b to a - result[k] = v + # if a is empty or equal to b, return b + if a == {} or a == b: + return b.copy() + + # if b is empty the below unfolds quickly + result = a.copy() + + # next, iterate over b keys and values + for k, v in b.iteritems(): + # if there's already such key in a + # and that key contains dict + if k in result and isinstance(result[k], dict): + # merge those dicts recursively + result[k] = merge_hash(result[k], v) + else: + # otherwise, just copy a value from b to a + result[k] = v return result |