From a1ad5ab74571b4ec13292ffbacdf5c99a13fff89 Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Fri, 1 May 2026 11:35:03 -0600 Subject: [PATCH] refactor: log-to-syslog in debug_flags will log internal trace msgs to syslog [citest_skip] If you set __network_debug_flags=log-to-syslog then the internal trace messages which are returned by the module will also be logged to syslog. This is useful for when the network role hangs and times out and is killed before those messages can be returned. The trace messages printed to syslog can help debug problems. Signed-off-by: Rich Megginson --- library/network_connections.py | 15 ++++++++++++++- tests/playbooks/tests_route_device.yml | 4 ++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/library/network_connections.py b/library/network_connections.py index e9c09af8d..2fd1a5a4a 100644 --- a/library/network_connections.py +++ b/library/network_connections.py @@ -1822,6 +1822,8 @@ def log( raise AssertionError("idx {0} is less than -1".format(idx)) self._log_idx += 1 self.run_results[idx]["log"].append((severity, msg, self._log_idx)) + if "log-to-syslog" in self.module.params["__debug_flags"]: + self.module.log("%s: %s: %s" % (LogLevel.fmt(severity), idx, msg)) if severity == LogLevel.ERROR: if force_fail or not ignore_errors: self.fail_json( @@ -2019,13 +2021,17 @@ def log_fatal(self, idx, msg, warn_traceback=False): ) def log(self, idx, severity, msg, warn_traceback=False, force_fail=False): + if len(self.connections) > idx: + ignore_errors = self.connection_ignore_errors(self.connections[idx]) + else: + ignore_errors = self._ignore_errors self.run_env.log( self.connections, idx, severity, msg, is_changed=self.is_changed_modified_system, - ignore_errors=self.connection_ignore_errors(self.connections[idx]), + ignore_errors=ignore_errors, warn_traceback=warn_traceback, force_fail=force_fail, ) @@ -2125,7 +2131,9 @@ def run(self): self.run_prepare() while self.check_mode_next() != CheckMode.DONE: if self.check_mode == CheckMode.REAL_RUN: + self.log(0, LogLevel.INFO, "Starting transaction") self.start_transaction() + self.log(0, LogLevel.INFO, "Started transaction") # Reasoning for this order: # For down/up profiles might need to be present, so do this first @@ -2146,11 +2154,15 @@ def run(self): self.run_action_down(idx) except Exception as error: if self.check_mode == CheckMode.REAL_RUN: + self.log(idx, LogLevel.INFO, "Rolling back transaction") self.rollback_transaction(idx, action, error) + self.log(0, LogLevel.INFO, "Rolled back transaction") raise if self.check_mode == CheckMode.REAL_RUN: + self.log(0, LogLevel.INFO, "Finishing transaction") self.finish_transaction() + self.log(0, LogLevel.INFO, "Finished transaction") def run_prepare(self): for idx, connection in enumerate(self.connections): @@ -2912,6 +2924,7 @@ def main(): changed=(cmd is not None and cmd.is_changed_modified_system), warn_traceback=not isinstance(e, MyError), ) + cmd.log(0, LogLevel.INFO, "Exiting") run_env_ansible.exit_json( connections, changed=(cmd is not None and cmd.is_changed_modified_system) ) diff --git a/tests/playbooks/tests_route_device.yml b/tests/playbooks/tests_route_device.yml index be6f16698..bd0d4c381 100644 --- a/tests/playbooks/tests_route_device.yml +++ b/tests/playbooks/tests_route_device.yml @@ -131,8 +131,8 @@ the output device is logged for initscripts provider assert: that: - - __network_connections_result.stderr is search("\[003\] - .0, state.None persistent_state.present, + - __network_connections_result.stderr is search(" .0, + state.None persistent_state.present, '{{ interface1 }}'. The connection {{ interface1 }} does not specify an interface name. Therefore, the route to 198.58.10.64/26 will be configured without the output device