selective callback plugin: route output through display to honour ANSIBLE_LOG_PATH (#11927)

* fix(selective): route output through display to honour ANSIBLE_LOG_PATH

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(selective): add changelog fragment for PR 11927

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Alexei Znamensky
2026-04-30 07:25:43 +12:00
committed by GitHub
parent 00cff7feb4
commit 41f2b5dec5
2 changed files with 19 additions and 14 deletions

View File

@@ -0,0 +1,5 @@
bugfixes:
- selective callback plugin - route all output through ``self._display.display()``
instead of bare ``print()`` calls, fixing missing output when ``ANSIBLE_LOG_PATH``
is set (https://github.com/ansible-collections/community.general/issues/4850,
https://github.com/ansible-collections/community.general/pull/11927).

View File

@@ -101,10 +101,10 @@ class CallbackModule(CallbackBase):
self.printed_last_task = True self.printed_last_task = True
line_length = 120 line_length = 120
if self.last_skipped: if self.last_skipped:
print() self._display.display("")
line = f"# {task_name} " line = f"# {task_name} "
msg = colorize(f"{line}{'*' * (line_length - len(line))}", "bold") msg = colorize(f"{line}{'*' * (line_length - len(line))}", "bold")
print(msg) self._display.display(msg)
def _indent_text(self, text, indent_level): def _indent_text(self, text, indent_level):
lines = text.splitlines() lines = text.splitlines()
@@ -128,7 +128,7 @@ class CallbackModule(CallbackBase):
diff = dict_diff(diff["before"], diff["after"]) diff = dict_diff(diff["before"], diff["after"])
if diff: if diff:
diff = colorize(str(diff), "changed") diff = colorize(str(diff), "changed")
print(self._indent_text(diff, indent_level + 4)) self._display.display(self._indent_text(diff, indent_level + 4))
def _print_host_or_item(self, host_or_item, changed, msg, diff, is_host, error, stdout, stderr): def _print_host_or_item(self, host_or_item, changed, msg, diff, is_host, error, stdout, stderr):
if is_host: if is_host:
@@ -156,19 +156,19 @@ class CallbackModule(CallbackBase):
if len(msg) < 50: if len(msg) < 50:
line += f" -- {msg}" line += f" -- {msg}"
print(f"{line} {'-' * (line_length - len(line))}---------") self._display.display(f"{line} {'-' * (line_length - len(line))}---------")
else: else:
print(f"{line} {'-' * (line_length - len(line))}") self._display.display(f"{line} {'-' * (line_length - len(line))}")
print(self._indent_text(msg, indent_level + 4)) self._display.display(self._indent_text(msg, indent_level + 4))
if diff: if diff:
self._print_diff(diff, indent_level) self._print_diff(diff, indent_level)
if stdout: if stdout:
stdout = colorize(stdout, "failed") stdout = colorize(stdout, "failed")
print(self._indent_text(stdout, indent_level + 4)) self._display.display(self._indent_text(stdout, indent_level + 4))
if stderr: if stderr:
stderr = colorize(stderr, "failed") stderr = colorize(stderr, "failed")
print(self._indent_text(stderr, indent_level + 4)) self._display.display(self._indent_text(stderr, indent_level + 4))
def v2_playbook_on_play_start(self, play): def v2_playbook_on_play_start(self, play):
"""Run on start of the play.""" """Run on start of the play."""
@@ -223,7 +223,7 @@ class CallbackModule(CallbackBase):
def v2_playbook_on_stats(self, stats): def v2_playbook_on_stats(self, stats):
"""Display info about playbook statistics.""" """Display info about playbook statistics."""
print() self._display.display("")
self.printed_last_task = False self.printed_last_task = False
self._print_task("STATS") self._print_task("STATS")
@@ -242,7 +242,7 @@ class CallbackModule(CallbackBase):
f"{host} : ok={s['ok']}\tchanged={s['changed']}\tfailed={s['failures']}\tunreachable=" f"{host} : ok={s['ok']}\tchanged={s['changed']}\tfailed={s['failures']}\tunreachable="
f"{s['unreachable']}\trescued={s['rescued']}\tignored={s['ignored']}" f"{s['unreachable']}\trescued={s['rescued']}\tignored={s['ignored']}"
) )
print(colorize(msg, color)) self._display.display(colorize(msg, color))
def v2_runner_on_skipped(self, result, **kwargs): def v2_runner_on_skipped(self, result, **kwargs):
"""Run when a task is skipped.""" """Run when a task is skipped."""
@@ -258,11 +258,11 @@ class CallbackModule(CallbackBase):
reason = result._result.get("skipped_reason", "") or result._result.get("skip_reason", "") reason = result._result.get("skipped_reason", "") or result._result.get("skip_reason", "")
if len(reason) < 50: if len(reason) < 50:
line += f" -- {reason}" line += f" -- {reason}"
print(f"{line} {'-' * (line_length - len(line))}---------") self._display.display(f"{line} {'-' * (line_length - len(line))}---------")
else: else:
print(f"{line} {'-' * (line_length - len(line))}") self._display.display(f"{line} {'-' * (line_length - len(line))}")
print(self._indent_text(reason, 8)) self._display.display(self._indent_text(reason, 8))
print(reason) self._display.display(reason)
def v2_runner_on_ok(self, result, **kwargs): def v2_runner_on_ok(self, result, **kwargs):
self._print_task_result(result, error=False, **kwargs) self._print_task_result(result, error=False, **kwargs)