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
This commit is contained in:
Sam Doran 2020-06-10 15:36:24 -04:00 committed by GitHub
parent 32c392e622
commit 3815ed67d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View file

@ -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
if remote_data:
role_info.update(remote_data)
raise AnsibleError("Unable to find info about '%s': %s" % (role, e))
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)

View file

@ -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"