diff --git a/changelogs/fragments/ansible-galaxy-role-list-specific-fix.yml b/changelogs/fragments/ansible-galaxy-role-list-specific-fix.yml new file mode 100644 index 00000000000000..eaef1d59523e8c --- /dev/null +++ b/changelogs/fragments/ansible-galaxy-role-list-specific-fix.yml @@ -0,0 +1,6 @@ +bugfixes: + - > + ansible-galaxy - fix a bug where listing a specific role if it was not in the first + path failed to find the role + + - ansible-galaxy - properly show the role description when running offline (https://github.com/ansible/ansible/issues/60167) diff --git a/lib/ansible/cli/galaxy.py b/lib/ansible/cli/galaxy.py index a5558e278459a0..dc072ac08a5674 100644 --- a/lib/ansible/cli/galaxy.py +++ b/lib/ansible/cli/galaxy.py @@ -580,7 +580,11 @@ def exit_without_ignore(rc=1): def _display_role_info(role_info): text = [u"", u"Role: %s" % to_text(role_info['name'])] - text.append(u"\tdescription: %s" % role_info.get('description', '')) + + # Get the top-level 'description' first, falling back to galaxy_info['galaxy_info']['description']. + galaxy_info = role_info.get('galaxy_info', {}) + description = role_info.get('description', galaxy_info.get('description', '')) + text.append(u"\tdescription: %s" % description) for k in sorted(role_info.keys()): @@ -854,6 +858,9 @@ def execute_info(self): role_info = {'path': roles_path} gr = GalaxyRole(self.galaxy, self.api, role) + if not gr._exists: + data = u"- the role %s was not found" % role + break install_info = gr.install_info if install_info: @@ -878,10 +885,6 @@ def execute_info(self): role_info.update(role_spec) data = self._display_role_info(role_info) - # FIXME: This is broken in both 1.9 and 2.0 as - # _display_role_info() always returns something - if not data: - data = u"\n- the role %s was not found" % role self.pager(data) diff --git a/lib/ansible/galaxy/role.py b/lib/ansible/galaxy/role.py index 8e1ca72fd42a46..ba12d1d4c28057 100644 --- a/lib/ansible/galaxy/role.py +++ b/lib/ansible/galaxy/role.py @@ -64,6 +64,7 @@ def __init__(self, galaxy, api, name, src=None, version=None, scm=None, path=Non self.version = version self.src = src or name self.scm = scm + self.paths = [os.path.join(x, self.name) for x in galaxy.roles_paths] if path is not None: if not path.endswith(os.path.join(os.path.sep, self.name)): @@ -82,9 +83,6 @@ def __init__(self, galaxy, api, name, src=None, version=None, scm=None, path=Non else: # use the first path by default self.path = os.path.join(galaxy.roles_paths[0], self.name) - # create list of possible paths - self.paths = [x for x in galaxy.roles_paths] - self.paths = [os.path.join(x, self.name) for x in self.paths] def __repr__(self): """ @@ -105,17 +103,17 @@ def metadata(self): Returns role metadata """ if self._metadata is None: - for meta_main in self.META_MAIN: - meta_path = os.path.join(self.path, meta_main) - if os.path.isfile(meta_path): - try: - f = open(meta_path, 'r') - self._metadata = yaml.safe_load(f) - except Exception: - display.vvvvv("Unable to load metadata for %s" % self.name) - return False - finally: - f.close() + for path in self.paths: + for meta_main in self.META_MAIN: + meta_path = os.path.join(path, meta_main) + if os.path.isfile(meta_path): + try: + with open(meta_path, 'r') as f: + self._metadata = yaml.safe_load(f) + except Exception: + display.vvvvv("Unable to load metadata for %s" % self.name) + return False + break return self._metadata @@ -138,6 +136,14 @@ def install_info(self): f.close() return self._install_info + @property + def _exists(self): + for path in self.paths: + if os.path.isdir(path): + return True + + return False + def _write_galaxy_install_info(self): """ Writes a YAML-formatted file to the role's meta/ directory diff --git a/test/integration/targets/ansible-galaxy/runme.sh b/test/integration/targets/ansible-galaxy/runme.sh index 2f735fb172bc48..00a7739d806216 100755 --- a/test/integration/targets/ansible-galaxy/runme.sh +++ b/test/integration/targets/ansible-galaxy/runme.sh @@ -120,6 +120,71 @@ f_ansible_galaxy_status \ [[ $(grep -c '^- test-role' out.txt ) -eq 2 ]] +# Galaxy role test case +# +# Test listing a specific role that is not in the first path in ANSIBLE_ROLES_PATH. +# https://github.com/ansible/ansible/issues/60167#issuecomment-585460706 + +f_ansible_galaxy_status \ + "list specific role not in the first path in ANSIBLE_ROLES_PATHS" + +role_testdir=$(mktemp -d) +pushd "${role_testdir}" + + mkdir testroles + ansible-galaxy role init --init-path ./local-roles quark + ANSIBLE_ROLES_PATH=./local-roles:${HOME}/.ansible/roles ansible-galaxy role list quark | tee out.txt + + [[ $(grep -c 'not found' out.txt) -eq 0 ]] + + ANSIBLE_ROLES_PATH=${HOME}/.ansible/roles:./local-roles ansible-galaxy role list quark | tee out.txt + + [[ $(grep -c 'not found' out.txt) -eq 0 ]] + +popd # ${role_testdir} +rm -fr "${role_testdir}" + + +# Galaxy role info tests + +f_ansible_galaxy_status \ + "role info non-existant role" + +role_testdir=$(mktemp -d) +pushd "${role_testdir}" + + ansible-galaxy role info notaroll | tee out.txt + + grep -- '- the role notaroll was not found' out.txt + +f_ansible_galaxy_status \ + "role info description offline" + + mkdir testroles + ansible-galaxy role init testdesc --init-path ./testroles + + # Only galaxy_info['description'] exists in file + sed -i -e 's#[[:space:]]\{1,\}description:.*$# description: Description in galaxy_info#' ./testroles/testdesc/meta/main.yml + ansible-galaxy role info -p ./testroles --offline testdesc | tee out.txt + grep 'description: Description in galaxy_info' out.txt + + # Both top level 'description' and galaxy_info['description'] exist in file + # Use shell-fu instead of sed to prepend a line to a file because BSD + # and macOS sed don't work the same as GNU sed. + echo 'description: Top level' | \ + cat - ./testroles/testdesc/meta/main.yml > tmp.yml && \ + mv tmp.yml ./testroles/testdesc/meta/main.yml + ansible-galaxy role info -p ./testroles --offline testdesc | tee out.txt + grep 'description: Top level' out.txt + + # Only top level 'description' exists in file + sed -i.bak '/^[[:space:]]\{1,\}description: Description in galaxy_info/d' ./testroles/testdesc/meta/main.yml + ansible-galaxy role info -p ./testroles --offline testdesc | tee out.txt + grep 'description: Top level' out.txt + +popd # ${role_testdir} +rm -fr "${role_testdir}" + # 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