Compile role with parents in the task's dependency chain (#75165)

* Use the task dependency chain to compile the role instead of the parent role's _parents list

* keep existing role inheritance, but limit it to roles in the current task dependency chain

* Test getting role parents recursively by using the current task dependency chain

* changelog
pull/75762/head
Sloane Hertel 3 years ago committed by GitHub
parent 7a76130502
commit 440cf15aeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,4 @@
bugfixes:
- include_role - Only inherit from role parents in the current task dependency chain (https://github.com/ansible/ansible/issues/39543).
- include_role - Inherit from role parents beyond a depth of 3 (https://github.com/ansible/ansible/issues/47023).
- include_role - Inherit from role parents in the order of the task dependency chain.

@ -100,11 +100,7 @@ class IncludeRole(TaskInclude):
# compile role with parent roles as dependencies to ensure they inherit
# variables
if not self._parent_role:
dep_chain = []
else:
dep_chain = list(self._parent_role._parents)
dep_chain.append(self._parent_role)
dep_chain = self.get_dep_chain() or []
p_block = self.build_parent_block()

@ -0,0 +1,7 @@
---
- hosts: all
gather_facts: no
roles:
- 39543_role1
- role: 39543_role3
when: false

@ -0,0 +1,5 @@
---
- hosts: all
gather_facts: no
tasks:
- include_role: name=47023_role1

@ -0,0 +1,5 @@
- debug:
msg: 'role1'
- include_role:
name: 39543_role2

@ -0,0 +1,5 @@
- debug:
msg: "Var is {{ my_var | default('undefined') }}"
- debug:
msg: "Default is {{ my_default | default('undefined') }}"

@ -17,3 +17,14 @@ set -eux
# ensure role data is merged correctly
ansible-playbook data_integrity.yml -i ../../inventory "$@"
# ensure role vars are inherited correctly
ANSIBLE_PRIVATE_ROLE_VARS=True ansible-playbook 39543.yml -i ../../inventory "$@" | tee out.txt
test "$(grep '"msg": "role1"' -c out.txt)" = "1"
test "$(grep '"msg": "role2"' -c out.txt)" = "1"
test "$(grep '"msg": "role3"' -c out.txt)" = "0"
# test nested includes get parent roles greater than a depth of 3
[ "$(ansible-playbook 47023.yml -i ../../inventory "$@" | grep '\<\(Default\|Var\)\>' | grep -c 'is defined')" = "2" ]

Loading…
Cancel
Save