From 3815ed67d53d3874d8f1b5d232ac77e127cab216 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Wed, 10 Jun 2020 15:36:24 -0400 Subject: [PATCH] ansible-galaxy - Fix role info when role is not installed (#69924) * ansible-galaxy - Fix role info when role is not installed Only report the role not found if in offline mode, otherwise query the galaxy API to get role information. Fixes #69867 * Improve error message when role is not found in Ansible Galaxy --- lib/ansible/cli/galaxy.py | 23 +++++++++++++------ .../targets/ansible-galaxy/runme.sh | 12 ++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/ansible/cli/galaxy.py b/lib/ansible/cli/galaxy.py index c1729bc458c..0680fab2410 100644 --- a/lib/ansible/cli/galaxy.py +++ b/lib/ansible/cli/galaxy.py @@ -927,9 +927,6 @@ class GalaxyCLI(CLI): 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: @@ -938,12 +935,24 @@ class GalaxyCLI(CLI): del install_info['version'] role_info.update(install_info) - remote_data = False if not context.CLIARGS['offline']: - remote_data = self.api.lookup_role_by_name(role, False) + remote_data = None + try: + remote_data = self.api.lookup_role_by_name(role, False) + except AnsibleError as e: + if e.http_code == 400 and 'Bad Request' in e.message: + # Role does not exist in Ansible Galaxy + data = u"- the role %s was not found" % role + break + + raise AnsibleError("Unable to find info about '%s': %s" % (role, e)) - if remote_data: - role_info.update(remote_data) + if remote_data: + role_info.update(remote_data) + + elif context.CLIARGS['offline'] and not gr._exists: + data = u"- the role %s was not found" % role + break if gr.metadata: role_info.update(gr.metadata) diff --git a/test/integration/targets/ansible-galaxy/runme.sh b/test/integration/targets/ansible-galaxy/runme.sh index d9d088154da..b7978b4e68f 100755 --- a/test/integration/targets/ansible-galaxy/runme.sh +++ b/test/integration/targets/ansible-galaxy/runme.sh @@ -226,6 +226,18 @@ rm -fr "${role_testdir}" # Galaxy role info tests +# Get info about role that is not installed + +f_ansible_galaxy_status "role info" +galaxy_testdir=$(mktemp -d) +pushd "${galaxy_testdir}" + ansible-galaxy role info samdoran.fish | tee out.txt + + [[ $(grep -c 'not found' out.txt ) -eq 0 ]] + [[ $(grep -c 'Role:.*samdoran\.fish' out.txt ) -eq 1 ]] + +popd # ${galaxy_testdir} + f_ansible_galaxy_status \ "role info non-existant role"