From 3e82ed307b4786ebe4dd5bb820d1c24877ad3b32 Mon Sep 17 00:00:00 2001 From: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> Date: Thu, 14 Nov 2024 13:50:14 -0500 Subject: [PATCH] include_vars - fix including new hash variables when hash_behaviour is set to merge (#84303) --- changelogs/fragments/fix-include_vars-merge-hash.yml | 2 ++ lib/ansible/plugins/action/include_vars.py | 5 ++--- test/integration/targets/include_vars/vars2/hashes/hash2.yml | 2 ++ 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/fix-include_vars-merge-hash.yml 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 00000000000..48f9bea0005 --- /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 693ef0ac4c4..38fe4a9f8e6 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 1f2a9636626..fa35a9f4e65 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 }