From cbddf102daf698052eba625e4030b013dd26f265 Mon Sep 17 00:00:00 2001 From: rbpittman Date: Tue, 5 Nov 2024 01:41:55 -0500 Subject: [PATCH] Ansible error formatting (#15208) * Revise ansible error formatting. * Revise to assume stdout_lines and stderr_lines are present. pre-commit. * Revert "Revise to assume stdout_lines and stderr_lines are present." This reverts commit 5f002ae9f92f266778f425bb1a80fb5a3904a924. * Revise to assume stdout_lines and stderr_lines are present. * Remove excess newlines. --- tests/common/errors.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/tests/common/errors.py b/tests/common/errors.py index 7f7d3f7cb98..d162ac26d5c 100644 --- a/tests/common/errors.py +++ b/tests/common/errors.py @@ -2,19 +2,31 @@ Customize exceptions """ from ansible.errors import AnsibleError -from ansible.plugins.loader import callback_loader class UnsupportedAnsibleModule(Exception): pass -def dump_ansible_results(results, stdout_callback='json'): - try: - cb = callback_loader.get(stdout_callback) - return cb._dump_results(results) if cb else results - except Exception: - return str(results) +def dump_ansible_results(results): + """Dump ansible results in a clean format. + Prints simple attributes printed first, followed by the stdout and stderr.""" + simple_attrs = "" + stdout = "stdout =\n" + stderr = "stderr =\n" + for key in results: + if key in ['stdout', 'stderr']: + # Use stdout_lines and stderr_lines instead + continue + if '_lines' in key: + text = "\n".join(results[key]) + if key == 'stdout_lines': + stdout += text + else: + stderr += text + else: + simple_attrs += "{} = {}\n".format(key, results[key]) + return "{}{}{}".format(simple_attrs, stdout, stderr) class RunAnsibleModuleFail(AnsibleError):