diff --git a/library/network_connections.py b/library/network_connections.py index a72f346..cd64abb 100644 --- a/library/network_connections.py +++ b/library/network_connections.py @@ -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) ) diff --git a/tasks/main.yml b/tasks/main.yml index 94c7680..4b36c5c 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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: diff --git a/tests/playbooks/tests_ethernet.yml b/tests/playbooks/tests_ethernet.yml index 0479947..cd02579 100644 --- a/tests/playbooks/tests_ethernet.yml +++ b/tests/playbooks/tests_ethernet.yml @@ -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: diff --git a/tests/playbooks/tests_reapply.yml b/tests/playbooks/tests_reapply.yml index adea4f5..4b1cb09 100644 --- a/tests/playbooks/tests_reapply.yml +++ b/tests/playbooks/tests_reapply.yml @@ -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 diff --git a/tests/tasks/assert_output_in_stderr_without_warnings.yml b/tests/tasks/assert_output_in_stderr_without_warnings.yml new file mode 100644 index 0000000..d760d3d --- /dev/null +++ b/tests/tasks/assert_output_in_stderr_without_warnings.yml @@ -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" diff --git a/tests/tests_default.yml b/tests/tests_default.yml index fda6ed5..f6f7550 100644 --- a/tests/tests_default.yml +++ b/tests/tests_default.yml @@ -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"