Skip to content

Commit

Permalink
Initial logic for printing logs on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Jan 29, 2024
1 parent 28c790a commit de89c30
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lookup_plugins/convert_to_utf16.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions lookup_plugins/file_utf16.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de89c30

Please sign in to comment.