Omar E
Omar E

Reputation: 550

Ansible: shell command returns blank is fatal

I'm trying to run a simple tail command with Ansible. Iterate over 3 strings until all are not found. If found loop over the tail command until all are not found.

   - name: Tail the logs for string
     shell: "tail -10 /path/to/log/file.log | egrep 'STRING1|STRING2|STRING3'"
     register: tail
     until: "'STRING1' and 'STRING2' and 'STRING3' not in tail.stdout_lines" 
     retries: 10
     delay: 5

When I run the above task and there's nothing to return, it exits with a fatal error. Even though this is the success case.

fatal: [testserver]: FAILED! => {"changed": true, "cmd": "tail -10 /path/to/log/file.log | egrep 'STRING1|STRING2|STRING3'", "delta": "0:00:00.012770", "end": "2016-09-07 07:44:35.684238", "failed": true, "invocation": {"module_args": {"_raw_params": "tail -10 /path/to/log/file.log | egrep 'STRING1|STRING2|STRING3'", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "rc": 1, "start": "2016-09-07 07:44:35.671468", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}

I am unsure why it ends in a fatal state.

Upvotes: 0

Views: 1611

Answers (2)

Omar E
Omar E

Reputation: 550

After resolving the tail command thanks the advice from @charles-duffy the issue remained with the loop condition.

Here is the correct condition: until: '"STRING1" not in tail.stdout and "STRING2" not in tail.stdout and "STRING3" not in tail.stdout'

I also used tail.stdout instead of tail.stdout_lines

Thanks

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295308

grep returns nonzero when it doesn't match anything. If you want to override its exit status:

shell: "tail -10 /path/to/log/file.log | egrep 'STRING1|STRING2|STRING3' ||:"

Upvotes: 2

Related Questions