summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--changelogs/fragments/fix-include_vars-merge-hash.yml2
-rw-r--r--lib/ansible/plugins/action/include_vars.py5
-rw-r--r--test/integration/targets/include_vars/vars2/hashes/hash2.yml2
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 }