From de89c305434e720dbff768842388380476dc0e69 Mon Sep 17 00:00:00 2001 From: kenorb Date: Mon, 29 Jan 2024 01:24:46 +0000 Subject: [PATCH] Initial logic for printing logs on failure --- lookup_plugins/convert_to_utf16.py | 17 +++++++++++++++++ lookup_plugins/file_utf16.py | 19 +++++++++++++++++++ tasks/main.yml | 17 +++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100755 lookup_plugins/convert_to_utf16.py create mode 100755 lookup_plugins/file_utf16.py diff --git a/lookup_plugins/convert_to_utf16.py b/lookup_plugins/convert_to_utf16.py new file mode 100755 index 0000000..fdbb842 --- /dev/null +++ b/lookup_plugins/convert_to_utf16.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +from ansible.errors import AnsibleError +from ansible.plugins.lookup import LookupBase +from ansible.module_utils.common.text.converters import to_text, to_native + + +class LookupModule(LookupBase): + def run(self, texts, variables=None, **kwargs): + ret = [] + + try: + for text in texts: + ret.append(to_text(text).decode("UTF-16")) + except Exception as e: + raise AnsibleError(to_native(repr(e))) + + return ret diff --git a/lookup_plugins/file_utf16.py b/lookup_plugins/file_utf16.py new file mode 100755 index 0000000..48b79ed --- /dev/null +++ b/lookup_plugins/file_utf16.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python +from ansible.errors import AnsibleError +from ansible.plugins.lookup import LookupBase +from ansible.module_utils.common.text.converters import to_text, to_native + + +class LookupModule(LookupBase): + def run(self, terms, variables=None, **kwargs): + ret = [] + + try: + for path in terms: + with open(path, "rb") as byte_stream: + text = byte_stream.read() + ret.append(to_text(text.decode("UTF-16"))) + except Exception as e: + raise AnsibleError(to_native(repr(e))) + + return ret diff --git a/tasks/main.yml b/tasks/main.yml index d79fafb..b0e67ac 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -74,6 +74,23 @@ msg: Test failed! - name: Make sure all handlers run ansible.builtin.meta: flush_handlers + - name: Prints logs + block: + - name: Finds all log files + ansible.builtin.find: + paths: "{{ mt_runner_mt_path }}" + patterns: "*.log" + recurse: true + register: mt_log_files + - name: Fetches log files + ansible.builtin.slurp: + src: "{{ item.path }}" + loop: "{{ mt_log_files.files }}" + register: mt_log_content + - name: Prints log file + ansible.builtin.debug: + msg: "{{ lookup('convert_to_utf16', item.content).split('\n') }}" + loop: "{{ mt_log_content.results }}" - name: Fail ansible.builtin.command: /bin/false changed_when: false