Make sure include_role inherit variables from parent role (#18627)

* Make sure include_role inherit variables from parent role

Setting the parent of task blocks generated by include_role after they
have been produced is not sufficient - it means the tasks don't have the
correct dependency chain set afterwards, and therefore, don't properly
inherit variables from outer roles.

In addition to manually setting the parents, pass the dep_chain when
compiling the role, such that variables are correctly imported.

Fixes #18540.

* Add tests for include_role

* Fix include_role variable inheritance for multiple parent levels
This commit is contained in:
Daniel Miranda 2016-11-28 20:54:27 -02:00 committed by Brian Coca
commit 57f4a9885e
2 changed files with 238 additions and 3 deletions

View file

@ -76,10 +76,16 @@ class IncludeRole(Task):
actual_role = Role.load(ri, myplay, parent_role=self._parent_role, from_files=self._from_files)
actual_role._metadata.allow_duplicates = self.allow_duplicates
# compile role
blocks = actual_role.compile(play=myplay)
# 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.extend(self._parent_role.get_all_dependencies())
dep_chain.append(self._parent_role)
# set parent to ensure proper inheritance
blocks = actual_role.compile(play=myplay, dep_chain=dep_chain)
for b in blocks:
b._parent = self