From 31ad786de168d55b7a2b20b5468e2d3f965b4021 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 1 Aug 2024 12:08:11 -0700 Subject: [PATCH] ansible-doc: handle on_fail (#83676) Handle errors raised when role doc has errors Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/ansible-doc.yml | 3 +++ lib/ansible/cli/doc.py | 10 ++++++++-- test/integration/targets/ansible-doc/runme.sh | 6 ++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/ansible-doc.yml diff --git a/changelogs/fragments/ansible-doc.yml b/changelogs/fragments/ansible-doc.yml new file mode 100644 index 00000000000..4e52017ac97 --- /dev/null +++ b/changelogs/fragments/ansible-doc.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - ansible-doc - handle no_fail condition for role. diff --git a/lib/ansible/cli/doc.py b/lib/ansible/cli/doc.py index 8a0fefa0224..a6a73b50b7b 100755 --- a/lib/ansible/cli/doc.py +++ b/lib/ansible/cli/doc.py @@ -387,6 +387,12 @@ class RoleMixin(object): for role, collection, role_path in (roles | collroles): argspec = self._load_argspec(role, role_path, collection) + if 'error' in argspec: + if fail_on_errors: + raise argspec['exception'] + else: + display.warning('Skipping role (%s) due to: %s' % (role, argspec['error']), True) + continue fqcn, doc = self._build_doc(role, role_path, collection, argspec, entry_point) if doc: result[fqcn] = doc @@ -887,6 +893,7 @@ class DocCLI(CLI, RoleMixin): plugin_type = context.CLIARGS['type'].lower() do_json = context.CLIARGS['json_format'] or context.CLIARGS['dump'] listing = context.CLIARGS['list_files'] or context.CLIARGS['list_dir'] + no_fail = bool(not context.CLIARGS['no_fail_on_errors']) if context.CLIARGS['list_files']: content = 'files' @@ -909,7 +916,6 @@ class DocCLI(CLI, RoleMixin): docs['all'] = {} for ptype in ptypes: - no_fail = bool(not context.CLIARGS['no_fail_on_errors']) if ptype == 'role': roles = self._create_role_list(fail_on_errors=no_fail) docs['all'][ptype] = self._create_role_doc(roles.keys(), context.CLIARGS['entry_point'], fail_on_errors=no_fail) @@ -935,7 +941,7 @@ class DocCLI(CLI, RoleMixin): if plugin_type == 'keyword': docs = DocCLI._get_keywords_docs(context.CLIARGS['args']) elif plugin_type == 'role': - docs = self._create_role_doc(context.CLIARGS['args'], context.CLIARGS['entry_point']) + docs = self._create_role_doc(context.CLIARGS['args'], context.CLIARGS['entry_point'], fail_on_errors=no_fail) else: # display specific plugin docs docs = self._get_plugins_docs(plugin_type, context.CLIARGS['args']) diff --git a/test/integration/targets/ansible-doc/runme.sh b/test/integration/targets/ansible-doc/runme.sh index 80149d708da..f7accb217cd 100755 --- a/test/integration/targets/ansible-doc/runme.sh +++ b/test/integration/targets/ansible-doc/runme.sh @@ -208,6 +208,12 @@ ANSIBLE_LIBRARY='./nolibrary' ansible-doc --metadata-dump --no-fail-on-errors -- output=$(ANSIBLE_LIBRARY='./nolibrary' ansible-doc --metadata-dump --playbook-dir broken-docs testns.testcol 2>&1 | grep -c 'ERROR!' || true) test "${output}" -eq 1 +# ensure that role doc does not fail when --no-fail-on-errors is supplied +ANSIBLE_LIBRARY='./nolibrary' ansible-doc --no-fail-on-errors --playbook-dir broken-docs testns.testcol.testrole -t role 1>/dev/null 2>&1 + +# ensure that role doc does fail when --no-fail-on-errors is not supplied +output=$(ANSIBLE_LIBRARY='./nolibrary' ansible-doc --playbook-dir broken-docs testns.testcol.testrole -t role 2>&1 | grep -c 'ERROR!' || true) +test "${output}" -eq 1 echo "testing legacy plugin listing" [ "$(ansible-doc -M ./library -l ansible.legacy |wc -l)" -gt "0" ]