diff options
author | Sloane Hertel <19572925+s-hertel@users.noreply.github.com> | 2024-11-14 19:50:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-14 19:50:14 +0100 |
commit | 3e82ed307b4786ebe4dd5bb820d1c24877ad3b32 (patch) | |
tree | 19d9eccf9e95be5a8214bd1dbc074f9bd94d6aea | |
parent | Sequence query fix (#83758) (diff) | |
download | ansible-3e82ed307b4786ebe4dd5bb820d1c24877ad3b32.tar.xz ansible-3e82ed307b4786ebe4dd5bb820d1c24877ad3b32.zip |
include_vars - fix including new hash variables when hash_behaviour is set to merge (#84303)
-rw-r--r-- | changelogs/fragments/fix-include_vars-merge-hash.yml | 2 | ||||
-rw-r--r-- | lib/ansible/plugins/action/include_vars.py | 5 | ||||
-rw-r--r-- | test/integration/targets/include_vars/vars2/hashes/hash2.yml | 2 |
3 files changed, 6 insertions, 3 deletions
diff --git a/changelogs/fragments/fix-include_vars-merge-hash.yml b/changelogs/fragments/fix-include_vars-merge-hash.yml new file mode 100644 index 0000000000..48f9bea000 --- /dev/null +++ b/changelogs/fragments/fix-include_vars-merge-hash.yml @@ -0,0 +1,2 @@ +bugfixes: + - include_vars - fix including previously undefined hash variables with hash_behaviour merge (https://github.com/ansible/ansible/issues/84295). diff --git a/lib/ansible/plugins/action/include_vars.py b/lib/ansible/plugins/action/include_vars.py index 693ef0ac4c..38fe4a9f8e 100644 --- a/lib/ansible/plugins/action/include_vars.py +++ b/lib/ansible/plugins/action/include_vars.py @@ -142,9 +142,8 @@ class ActionModule(ActionBase): result['message'] = err_msg elif self.hash_behaviour is not None and self.hash_behaviour != C.DEFAULT_HASH_BEHAVIOUR: merge_hashes = self.hash_behaviour == 'merge' - for key, value in results.items(): - old_value = task_vars.get(key, None) - results[key] = combine_vars(old_value, value, merge=merge_hashes) + existing_variables = {k: v for k, v in task_vars.items() if k in results} + results = combine_vars(existing_variables, results, merge=merge_hashes) result['ansible_included_var_files'] = self.included_files result['ansible_facts'] = results diff --git a/test/integration/targets/include_vars/vars2/hashes/hash2.yml b/test/integration/targets/include_vars/vars2/hashes/hash2.yml index 1f2a963662..fa35a9f4e6 100644 --- a/test/integration/targets/include_vars/vars2/hashes/hash2.yml +++ b/test/integration/targets/include_vars/vars2/hashes/hash2.yml @@ -3,3 +3,5 @@ config: key1: 1 key2: { b: 22 } key3: 3 +previously_undefined: + key1: { a: 1 } |