From 332f4985bf7490ad2435306c37b9424995f392e9 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 8 Aug 2023 11:45:57 -0400 Subject: [PATCH] simpler approach and dont override role collection --- lib/ansible/playbook/collectionsearch.py | 2 +- lib/ansible/playbook/role/definition.py | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/ansible/playbook/collectionsearch.py b/lib/ansible/playbook/collectionsearch.py index c94aed2e493..06604b30b94 100644 --- a/lib/ansible/playbook/collectionsearch.py +++ b/lib/ansible/playbook/collectionsearch.py @@ -47,7 +47,7 @@ class CollectionSearch: # this will only be called if someone specified a value; call the shared value _ensure_default_collection(collection_list=ds) - # ensure 'current's object collection is here', only playbooks and plays should have this property + # ensure 'current's object collection is here', only playbooks, plays and roles should have this property if getattr(self, '_collection', False) and self._collection not in ds: ds.insert(0, self._collection) diff --git a/lib/ansible/playbook/role/definition.py b/lib/ansible/playbook/role/definition.py index 49ab5767313..8c0d5c8c133 100644 --- a/lib/ansible/playbook/role/definition.py +++ b/lib/ansible/playbook/role/definition.py @@ -57,7 +57,10 @@ class RoleDefinition(Base, Conditional, Taggable, CollectionSearch): self._role_collection = None self._role_basedir = role_basedir self._role_params = dict() - self._collection_list = collection_list + if collection_list is None: + self._collection_list = [] + else: + self._collection_list = collection_list # def __repr__(self): # return 'ROLEDEF: ' + self._attributes.get('role', '') @@ -154,16 +157,12 @@ class RoleDefinition(Base, Conditional, Taggable, CollectionSearch): role_tuple = None - # ensure play collection context - if self._play._collection: - if not self._collection_list: - self._collection_list = [self._play._collection] - elif self._play._collection not in self._collection_list: - self._collection_list.insert(0, self._play._collection) - # try to load as a collection-based role first if self._collection_list or AnsibleCollectionRef.is_valid_fqcr(role_name): role_tuple = _get_collection_role_path(role_name, self._collection_list) + elif self._play._collection: + # ensure play collection context when role does not have one + role_tuple = _get_collection_role_path(role_name, [self._play._collection]) if role_tuple: # we found it, stash collection data and return the name/path tuple @@ -206,7 +205,7 @@ class RoleDefinition(Base, Conditional, Taggable, CollectionSearch): role_name = os.path.basename(role_name) return (role_name, role_path) - searches = (self._collection_list or []) + role_search_paths + searches = (self._collection_list) + role_search_paths raise AnsibleError("the role '%s' was not found in %s" % (role_name, ":".join(searches)), obj=self._ds) def _split_role_params(self, ds):