diff --git a/changelogs/fragments/1221-cli-json-errors.yml b/changelogs/fragments/1221-cli-json-errors.yml new file mode 100644 index 00000000..9798ff49 --- /dev/null +++ b/changelogs/fragments/1221-cli-json-errors.yml @@ -0,0 +1,3 @@ +bugfixes: + - "CLI-based modules - when parsing JSON output fails, also provide standard error output. Also provide information on the command and its result in machine-readable way + (https://github.com/ansible-collections/community.docker/issues/1216, https://github.com/ansible-collections/community.docker/pull/1221)." diff --git a/plugins/module_utils/_common_cli.py b/plugins/module_utils/_common_cli.py index 74e1a28f..6e7cc4b8 100644 --- a/plugins/module_utils/_common_cli.py +++ b/plugins/module_utils/_common_cli.py @@ -197,7 +197,11 @@ class AnsibleDockerClientBase: data = json.loads(stdout) except Exception as exc: # pylint: disable=broad-exception-caught self.fail( - f"Error while parsing JSON output of {self._compose_cmd_str(args)}: {exc}\nJSON output: {to_text(stdout)}" + f"Error while parsing JSON output of {self._compose_cmd_str(args)}: {exc}\nJSON output: {to_text(stdout)}\n\nError output:\n{to_text(stderr)}", + cmd=self._compose_cmd_str(args), + rc=rc, + stdout=stdout, + stderr=stderr, ) return rc, data, stderr @@ -223,7 +227,11 @@ class AnsibleDockerClientBase: result.append(json.loads(line)) except Exception as exc: # pylint: disable=broad-exception-caught self.fail( - f"Error while parsing JSON output of {self._compose_cmd_str(args)}: {exc}\nJSON output: {to_text(stdout)}" + f"Error while parsing JSON output of {self._compose_cmd_str(args)}: {exc}\nJSON output: {to_text(stdout)}\n\nError output:\n{to_text(stderr)}", + cmd=self._compose_cmd_str(args), + rc=rc, + stdout=stdout, + stderr=stderr, ) return rc, result, stderr