diff --git a/changelogs/fragments/67365-role-list-role-name-in-path.yml b/changelogs/fragments/67365-role-list-role-name-in-path.yml new file mode 100644 index 00000000000..a693d4fb491 --- /dev/null +++ b/changelogs/fragments/67365-role-list-role-name-in-path.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-galaxy - properly list roles when the role name also happens to be in the role path (https://github.com/ansible/ansible/issues/67365) diff --git a/lib/ansible/galaxy/role.py b/lib/ansible/galaxy/role.py index 0589562d143..8e1ca72fd42 100644 --- a/lib/ansible/galaxy/role.py +++ b/lib/ansible/galaxy/role.py @@ -66,8 +66,18 @@ class GalaxyRole(object): self.scm = scm if path is not None: - if self.name not in path: + if not path.endswith(os.path.join(os.path.sep, self.name)): path = os.path.join(path, self.name) + else: + # Look for a meta/main.ya?ml inside the potential role dir in case + # the role name is the same as parent directory of the role. + # + # Example: + # ./roles/testing/testing/meta/main.yml + for meta_main in self.META_MAIN: + if os.path.exists(os.path.join(path, name, meta_main)): + path = os.path.join(path, self.name) + break self.path = path else: # use the first path by default diff --git a/test/integration/targets/ansible-galaxy/runme.sh b/test/integration/targets/ansible-galaxy/runme.sh index 7eff3fd6b7e..2f735fb172b 100755 --- a/test/integration/targets/ansible-galaxy/runme.sh +++ b/test/integration/targets/ansible-galaxy/runme.sh @@ -108,7 +108,7 @@ popd # ${galaxy_testdir} rm -fr "${galaxy_testdir}" -# Galaxy role list test case +# Galaxy role list tests # # Basic tests to ensure listing roles works @@ -121,6 +121,33 @@ f_ansible_galaxy_status \ [[ $(grep -c '^- test-role' out.txt ) -eq 2 ]] +# Properly list roles when the role name is a subset of the path, or the role +# name is the same name as the parent directory of the role. Issue #67365 +# +# ./parrot/parrot +# ./parrot/arr +# ./testing-roles/test + +f_ansible_galaxy_status \ + "list roles where the role name is the same or a subset of the role path (#67365)" + +role_testdir=$(mktemp -d) +pushd "${role_testdir}" + + mkdir parrot + ansible-galaxy role init --init-path ./parrot parrot + ansible-galaxy role init --init-path ./parrot parrot-ship + ansible-galaxy role init --init-path ./parrot arr + + ansible-galaxy role list -p ./parrot | tee out.txt + + [[ $(grep -Ec '\- (parrot|arr)' out.txt) -eq 3 ]] + ansible-galaxy role list test-role | tee -a out.txt + +popd # ${role_testdir} +rm -rf "${role_testdir}" + + ################################# # ansible-galaxy collection tests #################################