diff options
author | Matt Martz <matt@sivel.net> | 2018-07-15 09:59:30 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-15 09:59:30 -0500 |
commit | 27b4d7ed31b6688253fc4089b7a6b97f2d548167 (patch) | |
tree | 4a26fb8b2ac14f3ab8c036de0b9fef13ddfc12f1 /lib/ansible/playbook/role/__init__.py | |
parent | 5864871fc17046ce37875dd6e43414b1cf381cd3 (diff) | |
download | ansible-27b4d7ed31b6688253fc4089b7a6b97f2d548167.tar.gz |
Add feature to expose vars/defaults with include/import_role (#41330)
* First pass at making 'private' work on include_role, imports are always public
* Prevent dupe task execution and overwriting handlers
* New functionality will use public instead of deprecated private
* Add tests for public exposure
* Validate vars before import/include to ensure they don't expose too early
* Add porting guide docs about public argument and change to import_role
* Add additional docs about public and vars exposure to module docs
* Insert role handlers at parse time, exposing them globally
Diffstat (limited to 'lib/ansible/playbook/role/__init__.py')
-rw-r--r-- | lib/ansible/playbook/role/__init__.py | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/lib/ansible/playbook/role/__init__.py b/lib/ansible/playbook/role/__init__.py index 18776a0953..51ee5a0b58 100644 --- a/lib/ansible/playbook/role/__init__.py +++ b/lib/ansible/playbook/role/__init__.py @@ -96,7 +96,7 @@ class Role(Base, Become, Conditional, Taggable): _delegate_to = FieldAttribute(isa='string') _delegate_facts = FieldAttribute(isa='bool', default=False) - def __init__(self, play=None, from_files=None): + def __init__(self, play=None, from_files=None, from_include=False): self._role_name = None self._role_path = None self._role_params = dict() @@ -108,6 +108,7 @@ class Role(Base, Become, Conditional, Taggable): self._dependencies = [] self._task_blocks = [] self._handler_blocks = [] + self._compiled_handler_blocks = None self._default_vars = dict() self._role_vars = dict() self._had_task_run = dict() @@ -117,6 +118,9 @@ class Role(Base, Become, Conditional, Taggable): from_files = {} self._from_files = from_files + # Indicates whether this role was included via include/import_role + self.from_include = from_include + super(Role, self).__init__() def __repr__(self): @@ -126,7 +130,7 @@ class Role(Base, Become, Conditional, Taggable): return self._role_name @staticmethod - def load(role_include, play, parent_role=None, from_files=None): + def load(role_include, play, parent_role=None, from_files=None, from_include=False): if from_files is None: from_files = {} @@ -153,7 +157,7 @@ class Role(Base, Become, Conditional, Taggable): role_obj.add_parent(parent_role) return role_obj - r = Role(play=play, from_files=from_files) + r = Role(play=play, from_files=from_files, from_include=from_include) r._load_role_data(role_include, parent_role=parent_role) if role_include.role not in play.ROLE_CACHE: @@ -359,7 +363,15 @@ class Role(Base, Become, Conditional, Taggable): return self._task_blocks[:] def get_handler_blocks(self, play, dep_chain=None): - block_list = [] + # Do not recreate this list each time ``get_handler_blocks`` is called. + # Cache the results so that we don't potentially overwrite with copied duplicates + # + # ``get_handler_blocks`` may be called when handling ``import_role`` during parsing + # as well as with ``Play.compile_roles_handlers`` from ``TaskExecutor`` + if self._compiled_handler_blocks: + return self._compiled_handler_blocks + + self._compiled_handler_blocks = block_list = [] # update the dependency chain here if dep_chain is None: |