Output Failed even if command gets executed successfully

550 views
Skip to first unread message

bablu

unread,
Feb 6, 2017, 3:27:52 AM2/6/17
to Ansible Project

How to ignore failed command or in case the result set has no output.  I need the output should be success if it won't find anything relevant mentioned in the condition.


/usr/bin/ansible node1 -a "grep 'value1' /tmp/test.log"    Actually there in no value1 exists in test.log. However, i am getting following output.

node1 | FAILED | rc=1 >>

However, the command ran successfully. Moreover, When i am changing it to the

/usr/bin/ansible node1 -a "grep 'value1' /tmp/test.log" -a  "ignore_errors= yes"

output is:

node1 | FAILED | rc=2 >>
[Errno 2] No such file or directory

While going through some forum found the following are the codes for it. However, is there any way i can get success in the the result set instead of FAIL.


 0       One or more processes were matched.
 1       No processes were matched.
 2       Invalid options were specified on the command line.
 3       An internal error occurred.


Johannes Kastl

unread,
Feb 6, 2017, 3:33:38 AM2/6/17
to ansible...@googlegroups.com
On 06.02.17 09:27 bablu wrote:
>
> How to ignore failed command or in case the result set has no output. I
> need the output should be success if it won't find anything relevant
> mentioned in the condition.

failed_when is your friend.
https://docs.ansible.com/ansible/playbooks_error_handling.html#controlling-what-defines-failure

> node1 | FAILED | rc=1 >>

That is because grep returns that error.

> /usr/bin/ansible node1 -a "grep 'value1' /tmp/test.log" -a
> "ignore_errors= yes"

Make that an -e instead of an -a and try again. But I am not sure if
this is only valid in playbooks or if it can be used with ansible
ad-hoc commands.

> node1 | FAILED | rc=2 >>
> [Errno 2] No such file or directory

This is because you made the command look like this:
grep 'value1 ignore_errors= yes

Johannes

signature.asc

bablu

unread,
Feb 6, 2017, 4:21:55 AM2/6/17
to Ansible Project
Tried -e its giving the same output as with the -a

Any example how to use failed_when in ad-hoc command.

e.g i was trying below. How can i use failed_when here.

/usr/bin/ansible node1 -a "grep 'value1' /tmp/test.log"



DHAVAL JAISWAL

unread,
Feb 9, 2017, 1:36:59 PM2/9/17
to ansible...@googlegroups.com
I want to use the failed_when in following ad-hoc command to ignore the error. Is there any other alternative can be handle in ad hoc command.

/usr/bin/ansible node1 -a "grep 'value1' /tmp/test.log"

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/_NnsHPwj5kU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/12a10d0e-c6e9-4d1e-921f-69fb715b3d30%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Thanks & Regards
Dhaval Jaiswal

Johannes Kastl

unread,
Feb 9, 2017, 1:42:37 PM2/9/17
to ansible...@googlegroups.com
On 09.02.17 19:36 DHAVAL JAISWAL wrote:
> I want to use the failed_when in following ad-hoc command to ignore
> the error. Is there any other alternative can be handle in ad hoc
> command.
>
> /usr/bin/ansible node1 -a "grep 'value1' /tmp/test.log"

As nobody came up with a solution, I would assume that failed_when is
only applicable to tasks, not to ad-hoc commands.

Workaround: Use the shell module and make your command exit with an
error != 1:

/usr/bin/ansible node1 -m shell -a "grep 'value1' /tmp/test.log || exit 0"


Johannes

signature.asc

DHAVAL JAISWAL

unread,
Feb 10, 2017, 11:30:57 AM2/10/17
to ansible...@googlegroups.com
It works !!

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/_NnsHPwj5kU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages