# Output Failed even if command gets executed successfully

**URL:** <https://forum.ansible.com/t/output-failed-even-if-command-gets-executed-successfully/23644>\
**Category:** Ansible Project\
**Created:** [February 6, 2017, 8:27am UTC](https://forum.ansible.com/t/output-failed-even-if-command-gets-executed-successfully/23644 "2017-02-06T08:27:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 6, 2017, 8:27am UTC](https://forum.ansible.com/t/output-failed-even-if-command-gets-executed-successfully/23644/1 "2017-02-06T08:27:52Z")

</div>

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.

```auto
 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.

```

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 6, 2017, 8:33am UTC](https://forum.ansible.com/t/output-failed-even-if-command-gets-executed-successfully/23644/2 "2017-02-06T08:33:19Z")

</div>

> 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](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

---

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 6, 2017, 9:21am UTC](https://forum.ansible.com/t/output-failed-even-if-command-gets-executed-successfully/23644/3 "2017-02-06T09:21:54Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 9, 2017, 6:36pm UTC](https://forum.ansible.com/t/output-failed-even-if-command-gets-executed-successfully/23644/4 "2017-02-09T18:36:53Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 9, 2017, 6:42pm UTC](https://forum.ansible.com/t/output-failed-even-if-command-gets-executed-successfully/23644/5 "2017-02-09T18:42:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 10, 2017, 4:30pm UTC](https://forum.ansible.com/t/output-failed-even-if-command-gets-executed-successfully/23644/6 "2017-02-10T16:30:51Z")

</div>

It works !!
