From a41f09901bfb2bc5bcd222818c637436dc01a861 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Tue, 14 Apr 2020 21:18:23 -0400 Subject: [PATCH] [stable-2.9] Fix missing persistent connection messages (#68496) (#68562) * [stable-2.9] Fix missing persistent connection messages (#68496) * Be more proactive about returning module messages * Move message display to a function, and replace handling already in shutdown(). (cherry picked from commit 5f6427b1fc7449a5c42212013d3f628665701c3d) Co-authored-by: Nathaniel Case * Add changelog --- .../fragments/68496-persistent-logging.yaml | 3 +++ .../scripts/ansible_connection_cli_stub.py | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 changelogs/fragments/68496-persistent-logging.yaml diff --git a/changelogs/fragments/68496-persistent-logging.yaml b/changelogs/fragments/68496-persistent-logging.yaml new file mode 100644 index 00000000000..1be46de140a --- /dev/null +++ b/changelogs/fragments/68496-persistent-logging.yaml @@ -0,0 +1,3 @@ +bugfixes: +- Log additional messages from persistent connection modules that may be + missed if the module fails or returns early. diff --git a/lib/ansible/cli/scripts/ansible_connection_cli_stub.py b/lib/ansible/cli/scripts/ansible_connection_cli_stub.py index 487a00a52fc..17d1979abf4 100755 --- a/lib/ansible/cli/scripts/ansible_connection_cli_stub.py +++ b/lib/ansible/cli/scripts/ansible_connection_cli_stub.py @@ -147,6 +147,8 @@ class ConnectionProcess(object): resp = self.srv.handle_request(data) signal.alarm(0) + display_messages(self.connection) + if log_messages: display.display("jsonrpc response: %s" % resp, log_only=True) @@ -196,6 +198,7 @@ class ConnectionProcess(object): self.sock.close() if self.connection: self.connection.close() + display_messages(self.connection) except Exception: pass finally: @@ -332,6 +335,24 @@ def main(): sys.exit(rc) +def display_messages(connection): + # This should be handled elsewhere, but if this is the last task, nothing will + # come back to collect the messages. So now each task will dump its own messages + # to stdout before logging the response message. This may make some other + # pop_messages calls redundant. + for level, message in connection.pop_messages(): + if connection.get_option('persistent_log_messages') and level == "log": + display.display(message, log_only=True) + else: + # These should be keyed by valid method names, but + # fail gracefully just in case. + display_method = getattr(display, level, None) + if display_method: + display_method(message) + else: + display.display((level, message)) + + if __name__ == '__main__': display = Display() main()