Ansible 2.8 hanging with bash command

I have a task that searches logs for specific errors in various logs (up to 7 days worth of logs). When searching through just one log I never had an issue, but when searching through the 7 logs (used a wildcard to accomplish this), sometimes Ansible goes to lunch and doesn’t recover. It just sits there like it is still executing. We have let it run for many hours in that state just to make sure. I think it has a lot to do with timing and/or the size of the files (and the number of errors found int he files). Wondering if there is a more robust way to address this so that it will either not hang, continue on after a certain point (maybe a 5 minute timeout) or be more efficient (such as searching logs and writing to a file with an ansible module I don’t know of)?

`

  • name: Gather error reports
    shell: |
    while read line; do
    grep -i “$line” “{{ log_source }}”* | uniq > “{{ dest_dir }}/{{ ansible_hostname }}-$line.txt”
    done < “{{ error_log }}”
    failed_when: false
    args:
    executable: /bin/bash

`

I am using the following. Not sure if there is a better solution:

`

  • name: Gather error reports in background
    shell: |
    while read line; do
    grep -i “$line” “{{ log_source }}”* | uniq > “{{ dest_dir }}/{{ ansible_hostname }}-$line.txt”
    done < “{{ error_log }}”
    failed_when: false
    args:
    executable: /bin/bash
    async: 180
    poll: 0
    register: gather_sleeper

  • name: Check on report gathering – This can take a while
    async_status:
    jid: “{{ gather_sleeper.ansible_job_id }}”
    register: job_result
    until: job_result.finished
    retries: 37
    failed_when: false

`

I have a task that searches logs for specific errors in various logs (up to 7 days worth of logs). When searching
through just one log I never had an issue, but when searching through the 7 logs (used a wildcard to accomplish this),
sometimes Ansible goes to lunch and doesn't recover. It just sits there like it is still executing. We have let it run
for many hours in that state just to make sure. I think it has a lot to do with timing and/or the size of the files
(and the number of errors found int he files). Wondering if there is a more robust way to address this so that it will
either not hang, continue on after a certain point (maybe a 5 minute timeout) or be more efficient (such as searching
logs and writing to a file with an ansible module I don't know of)?

>
- name: Gather error reports
shell: |
while read line; do
grep -i "$line" "{{ log_source }}"* | uniq > "{{ dest_dir }}/{{ ansible_hostname }}-$line.txt"
done < "{{ error_log }}"
failed_when: false
args:
executable: /bin/bash
>

I would recommend to use a dedicated tool such as logwatch.

Regards
         Racke