library: Separate debug and info logs from warnings

Logs are now separed by severity level. Warnings and failures are the only logs
that appear now on the output. All logs are saved into a new json parameter
called "stderr" that is later shown on a different task. In case of
failure, all logs are shown as output. Tests have been created and modified in
order to assure that this feature works.

Signed-off-by: Elvira Garcia Ruiz <elviragr@riseup.net>
This commit is contained in:
Elvira Garcia Ruiz 2020-04-26 02:19:25 +02:00 committed by Till Maas
parent b5fb076579
commit 4753c70c55
6 changed files with 46 additions and 14 deletions

View file

@ -1559,24 +1559,32 @@ class RunEnvironmentAnsible(RunEnvironment):
c["persistent_state"],
)
prefix = prefix + (", '%s'" % (c["name"]))
for r in rr["log"]:
yield (r[2], "[%03d] %s %s: %s" % (r[2], LogLevel.fmt(r[0]), prefix, r[1]))
def _complete_kwargs(self, connections, kwargs, traceback_msg=None):
if "warnings" in kwargs:
logs = list(kwargs["warnings"])
else:
logs = []
for severity, msg, idx in rr["log"]:
yield (
idx,
"[%03d] %s %s: %s" % (idx, LogLevel.fmt(severity), prefix, msg),
severity,
)
def _complete_kwargs(self, connections, kwargs, traceback_msg=None, fail=False):
warning_logs = kwargs.get("warnings", [])
debug_logs = []
loglines = []
for res in self._run_results:
for idx, rr in enumerate(res):
loglines.extend(self._complete_kwargs_loglines(rr, connections, idx))
loglines.sort(key=lambda x: x[0])
logs.extend([x[1] for x in loglines])
loglines.sort(key=lambda log_line: log_line[0])
for idx, log_line, severity in loglines:
debug_logs.append(log_line)
if fail:
warning_logs.append(log_line)
elif severity >= LogLevel.WARN:
warning_logs.append(log_line)
if traceback_msg is not None:
logs.append(traceback_msg)
kwargs["warnings"] = logs
warning_logs.append(traceback_msg)
kwargs["warnings"] = warning_logs
stderr = "\n".join(debug_logs) + "\n"
kwargs["stderr"] = stderr
return kwargs
def exit_json(self, connections, changed=False, **kwargs):
@ -1595,7 +1603,7 @@ class RunEnvironmentAnsible(RunEnvironment):
kwargs["msg"] = msg
kwargs["changed"] = changed
self.module.fail_json(
**self._complete_kwargs(connections, kwargs, traceback_msg)
**self._complete_kwargs(connections, kwargs, traceback_msg, fail=True)
)

View file

@ -64,6 +64,10 @@
ignore_errors: "{{ network_ignore_errors | default(omit) }}"
force_state_change: "{{ network_force_state_change | default(omit) }}"
connections: "{{ network_connections | default([]) }}"
register: __network_connections_result
- name: Show debug messages
debug: var=__network_connections_result
- name: Re-test connectivity
ping:

View file

@ -37,6 +37,8 @@
address: 192.0.2.1/24
roles:
- linux-system-roles.network
tasks:
- include_tasks: tasks/assert_output_in_stderr_without_warnings.yml
- hosts: all
tasks:

View file

@ -50,7 +50,7 @@
- name: Assert that reapply is found in log output
assert:
fail_msg: Reapply not found in log output
that: "{{ 'connection reapplied' in test_module_run.warnings[2] }}"
that: "{{ 'connection reapplied' in test_module_run.stderr }}"
always:
- block:
# Use internal module directly for speedup

View file

@ -0,0 +1,12 @@
# SPDX-License-Identifier: BSD-3-Clause
---
- name: "Assert that warnings is empty"
assert:
that:
- "'warnings' not in __network_connections_result"
msg: "There are unexpected warnings"
- name: "Assert that there is output in stderr"
assert:
that:
- "'stderr' in __network_connections_result"
msg: "There are no messages in stderr"

View file

@ -4,3 +4,9 @@
hosts: all
roles:
- linux-system-roles.network
tasks:
- name: Test warning and info logs
assert:
that:
- "'warnings' not in __network_connections_result"
msg: "There are warnings"