From da98fc267a8f8e830069ab277fe5d9593b76e54f Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Wed, 22 Apr 2020 13:58:08 -0500 Subject: [PATCH] Allow a collection role to call a standalone role by default (#69102) * Allow a collection role to call a standalone role by default. Fixes #69101 * tweaked changelog text * Guard against NoneType Co-authored-by: Matt Davis --- .../69101-collection-role-to-standalone-role.yml | 5 +++++ lib/ansible/playbook/role/definition.py | 9 +++++---- .../testns/testcoll/roles/call_standalone/tasks/main.yml | 6 ++++++ test/integration/targets/collections/posix.yml | 5 +++++ .../targets/collections/roles/standalone/tasks/main.yml | 2 ++ 5 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/69101-collection-role-to-standalone-role.yml create mode 100644 test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/roles/call_standalone/tasks/main.yml create mode 100644 test/integration/targets/collections/roles/standalone/tasks/main.yml diff --git a/changelogs/fragments/69101-collection-role-to-standalone-role.yml b/changelogs/fragments/69101-collection-role-to-standalone-role.yml new file mode 100644 index 00000000000..04e8275af97 --- /dev/null +++ b/changelogs/fragments/69101-collection-role-to-standalone-role.yml @@ -0,0 +1,5 @@ +bugfixes: +- Collections - Allow a collection role to call a stand alone role, without + needing to explicitly add ``ansible.legacy`` to the collection search + order within the collection role. + (https://github.com/ansible/ansible/issues/69101) diff --git a/lib/ansible/playbook/role/definition.py b/lib/ansible/playbook/role/definition.py index 1f154c694cf..b859f63a956 100644 --- a/lib/ansible/playbook/role/definition.py +++ b/lib/ansible/playbook/role/definition.py @@ -162,9 +162,9 @@ class RoleDefinition(Base, Conditional, Taggable, CollectionSearch): self._role_collection = role_tuple[2] return role_tuple[0:2] - # FUTURE: refactor this to be callable from internal so we can properly order ansible.legacy searches with the collections keyword - if self._collection_list and 'ansible.legacy' not in self._collection_list: - raise AnsibleError("the role '%s' was not found in %s" % (role_name, ":".join(self._collection_list)), obj=self._ds) + # We didn't find a collection role, look in defined role paths + # FUTURE: refactor this to be callable from internal so we can properly order + # ansible.legacy searches with the collections keyword # we always start the search for roles in the base directory of the playbook role_search_paths = [ @@ -198,7 +198,8 @@ class RoleDefinition(Base, Conditional, Taggable, CollectionSearch): role_name = os.path.basename(role_name) return (role_name, role_path) - raise AnsibleError("the role '%s' was not found in %s" % (role_name, ":".join(role_search_paths)), obj=self._ds) + searches = (self._collection_list or []) + 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): ''' diff --git a/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/roles/call_standalone/tasks/main.yml b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/roles/call_standalone/tasks/main.yml new file mode 100644 index 00000000000..f5dcc0fccc8 --- /dev/null +++ b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/roles/call_standalone/tasks/main.yml @@ -0,0 +1,6 @@ +- include_role: + name: standalone + +- assert: + that: + - standalone_role_var is defined diff --git a/test/integration/targets/collections/posix.yml b/test/integration/targets/collections/posix.yml index d1ea2f9c04a..0d7c7089c4b 100644 --- a/test/integration/targets/collections/posix.yml +++ b/test/integration/targets/collections/posix.yml @@ -401,3 +401,8 @@ handler_counter: 0 roles: - testns.testcoll.test_fqcn_handlers + +- name: Ensure a collection role can call a standalone role + hosts: testhost + roles: + - testns.testcoll.call_standalone diff --git a/test/integration/targets/collections/roles/standalone/tasks/main.yml b/test/integration/targets/collections/roles/standalone/tasks/main.yml new file mode 100644 index 00000000000..b4dd23db98b --- /dev/null +++ b/test/integration/targets/collections/roles/standalone/tasks/main.yml @@ -0,0 +1,2 @@ +- set_fact: + standalone_role_var: True