Skip to content

Commit

Permalink
Ansible error formatting (#15208)
Browse files Browse the repository at this point in the history
* 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 5f002ae.

* Revise to assume stdout_lines and stderr_lines are present.

* Remove excess newlines.
  • Loading branch information
rbpittman authored and mssonicbld committed Nov 5, 2024
1 parent 5cd9912 commit c982adc
Showing 1 changed file with 19 additions and 7 deletions.
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

0 comments on commit c982adc

Please sign in to comment.