Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible shell module returns error when grep results are empty

Tags:

grep

ansible

I am using Ansible's shell module to find a particular string and store it in a variable. But if grep did not find anything I am getting an error.

Example:

- name: Get the http_status   shell: grep "http_status=" /var/httpd.txt   register: cmdln   check_mode: no 

When I run this Ansible playbook if http_status string is not there, playbook is stopped. I am not getting stderr.

How can I make Ansible run without interruption even if the string is not found?

like image 437
SSN Avatar asked Dec 07 '16 05:12

SSN


People also ask

How do you grep Ansible output?

There is no Ansible grep module, but you can use the grep commands along with shell module or command module. We can store the results of the task and use it in various conditional statements, print them or use them for debugging purposes.

What is non zero return code?

Non-zero return codes are normally used for failure cases with a 0 return code used for a successful command so this can give the impression the command has failed when it has not.

What is the difference between shell and command module in Ansible?

The shell module executes commands in nodes or Shell scripts. Another dedicated Ansible module is Script that transfers the Shell script from the control machine to the remote server and executes it. In the command module, the given command executes on all selected nodes.

What is the use of shell module in Ansible?

The shell module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the command module but runs the command through a shell ( /bin/sh ) on the remote node. For Windows targets, use the win_shell module instead.


2 Answers

grep by design returns code 1 if the given string was not found. Ansible by design stops execution if the return code is different from 0. Your system is working properly.

To prevent Ansible from stopping playbook execution on this error, you can:

  • add ignore_errors: yes parameter to the task

  • use failed_when: parameter with a proper condition

Because grep returns error code 2 for exceptions, the second method seems more appropriate, so:

- name: Get the http_status   shell: grep "http_status=" /var/httpd.txt   register: cmdln   failed_when: "cmdln.rc == 2"   check_mode: no 

You might also consider adding changed_when: false so that the task won't be reported as "changed" every single time.

All options are described in the Error Handling In Playbooks document.

like image 193
techraf Avatar answered Nov 02 '22 19:11

techraf


Like you observed, ansible will stop execution if the grep exit code is not zero. You can ignore it with ignore_errors.

Another trick is to pipe the grep output to cat. So cat exit code will always be zero since its stdin is grep's stdout. It works if there is a match and also when there is no match. Try it.

- name: Get the http_status   shell: grep "http_status=" /var/httpd.txt | cat   register: cmdln   check_mode: no 
like image 24
helloV Avatar answered Nov 02 '22 20:11

helloV