diff --git a/changelogs/fragments/fix_block_var_inh.yml b/changelogs/fragments/fix_block_var_inh.yml new file mode 100644 index 00000000000..b1d2560c1ec --- /dev/null +++ b/changelogs/fragments/fix_block_var_inh.yml @@ -0,0 +1,2 @@ +bugfixes: + - correctly inherit vars from parent in block (https://github.com/ansible/ansible/issues/75286). diff --git a/lib/ansible/playbook/block.py b/lib/ansible/playbook/block.py index 3c8ecc76291..a9b399f2f9f 100644 --- a/lib/ansible/playbook/block.py +++ b/lib/ansible/playbook/block.py @@ -79,11 +79,13 @@ class Block(Base, Conditional, CollectionSearch, Taggable): of a role or task include which does, so return those if present. ''' - all_vars = self.vars.copy() + all_vars = {} if self._parent: all_vars.update(self._parent.get_vars()) + all_vars.update(self.vars.copy()) + return all_vars @staticmethod diff --git a/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/roles/non_coll_role/tasks/main.yml b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/roles/non_coll_role/tasks/main.yml index d41ae90e95d..3fab7fe977a 100644 --- a/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/roles/non_coll_role/tasks/main.yml +++ b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/roles/non_coll_role/tasks/main.yml @@ -26,4 +26,4 @@ - assert: that: - - test_role_output.msg == test_role_input + - non_coll_role_to_call_test_role_output.msg == test_role_input diff --git a/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/roles/non_coll_role_to_call/tasks/main.yml b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/roles/non_coll_role_to_call/tasks/main.yml index 98445ce3aaf..2b1c15f2320 100644 --- a/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/roles/non_coll_role_to_call/tasks/main.yml +++ b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/roles/non_coll_role_to_call/tasks/main.yml @@ -1,7 +1,7 @@ - debug: msg: '{{ test_role_input | default("(undefined)") }}' - register: test_role_output + register: non_coll_role_to_call_test_role_output - assert: that: - - test_role_input is not defined or test_role_input == test_role_output.msg + - 'non_coll_role_to_call_test_role_output.msg == "include another non-coll role"' diff --git a/test/integration/targets/var_inheritance/aliases b/test/integration/targets/var_inheritance/aliases new file mode 100644 index 00000000000..498fedd558e --- /dev/null +++ b/test/integration/targets/var_inheritance/aliases @@ -0,0 +1,2 @@ +shippable/posix/group4 +context/controller diff --git a/test/integration/targets/var_inheritance/tasks/main.yml b/test/integration/targets/var_inheritance/tasks/main.yml new file mode 100644 index 00000000000..48d7b3d3a8c --- /dev/null +++ b/test/integration/targets/var_inheritance/tasks/main.yml @@ -0,0 +1,16 @@ +- name: outer + vars: + myvar: abc + block: + - assert: + that: + - "myvar == 'abc'" + + - name: inner block + vars: + myvar: 123 + block: + + - assert: + that: + - myvar|int == 123