From ee112d9984d52983afcf44061ce850c3dfb4aacf Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 16 Feb 2022 11:57:25 -0500 Subject: [PATCH] fix block var inheritance (#75287) (#76897) * updated tests to conform to new block inheritance Co-authored-by: Brian Scholer <1260690+briantist@users.noreply.github.com> Co-authored-by: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> (cherry picked from commit b1d6750e8bbdf507a4af24a4319b78b7287e233c) --- changelogs/fragments/fix_block_var_inh.yml | 2 ++ lib/ansible/playbook/block.py | 4 +++- .../playbooks/roles/non_coll_role/tasks/main.yml | 2 +- .../roles/non_coll_role_to_call/tasks/main.yml | 4 ++-- test/integration/targets/var_inheritance/aliases | 2 ++ .../targets/var_inheritance/tasks/main.yml | 16 ++++++++++++++++ 6 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/fix_block_var_inh.yml create mode 100644 test/integration/targets/var_inheritance/aliases create mode 100644 test/integration/targets/var_inheritance/tasks/main.yml 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