compile role with all parents in the dep chain (#79079)

fix inheriting from parents when role depth exceeds 3

Fixes #47023
pull/79122/head
Sloane Hertel 2 years ago committed by GitHub
parent cc2e7501db
commit f9cb679675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- include_role - Inherit from role parents beyond a depth of 3 (https://github.com/ansible/ansible/issues/47023).

@ -442,6 +442,13 @@ class Role(Base, Conditional, Taggable, CollectionSearch):
def get_parents(self):
return self._parents
def get_dep_chain(self):
dep_chain = []
for parent in self._parents:
dep_chain.extend(parent.get_dep_chain())
dep_chain.append(parent)
return dep_chain
def get_default_vars(self, dep_chain=None):
dep_chain = [] if dep_chain is None else dep_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 = actual_role.get_dep_chain()
p_block = self.build_parent_block()

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

@ -0,0 +1,6 @@
---
- hosts: all
tasks:
- name: static import inside dynamic include inherits defaults/vars
include_role:
name: include_import_dep_chain

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

@ -0,0 +1,4 @@
- assert:
that:
- inherit_var is defined
- inherit_default is defined

@ -29,3 +29,9 @@ fi
# ensure vars scope is correct
ansible-playbook vars_scope.yml -i ../../inventory "$@"
# 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" ]
# ensure import_role called from include_role has the include_role in the dep chain
ansible-playbook role_dep_chain.yml -i ../../inventory "$@"

Loading…
Cancel
Save