Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[action] [PR:15208] Ansible error formatting #15362

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions tests/common/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading