# Ansible 2.2.0 - Module shell & win\_shell - possible issue when output is empty

**URL:** <https://forum.ansible.com/t/ansible-2-2-0-module-shell-win-shell-possible-issue-when-output-is-empty/23048>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [November 17, 2016, 9:36am UTC](https://forum.ansible.com/t/ansible-2-2-0-module-shell-win-shell-possible-issue-when-output-is-empty/23048 "2016-11-17T09:36:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabrice.perko](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@fabrice.perko](https://forum.ansible.com/u/fabrice.perko)\
**Post date:** [November 17, 2016, 9:36am UTC](https://forum.ansible.com/t/ansible-2-2-0-module-shell-win-shell-possible-issue-when-output-is-empty/23048/1 "2016-11-17T09:36:53Z")

</div>

Hello,

I encounter a bizarre behavior with these the shell module and the new win\_shell module.

It appears that, if the execution of the shell command does not produce anything in the standard output, the module considers that it is in failure.

In my opinion, this should not be the case. Is this behavior an error or is it voluntary?

I can use “ignore\_errors: yes” to continue the playbook but it is not very clean.

Here are some tasks to illustrate the problem :

# ----------------------------------

- name: “Use Case 1”  
shell: grep “log” /etc/fstab  
ignore\_errors: yes  
register: shell\_result

- debug: var=shell\_result

# ----------------------------------

- name: “Use Case 2”  
shell: grep “xxx” /etc/fstab  
ignore\_errors: yes  
register: shell\_result

- debug: var=shell\_result

# ----------------------------------

- name: “Use Case 3”  
shell: grep “log” /etc/fstab \> /tmp/test\_ansible/use\_case\_3\_result.txt  
ignore\_errors: yes  
register: shell\_result

- debug: var=shell\_result

# ----------------------------------

- name: “Use Case 4”  
shell: grep “xxx” /etc/fstab \> /tmp/test\_ansible/use\_case\_4\_result.txt  
ignore\_errors: yes  
register: shell\_result

- debug: var=shell\_result

Here are the results :

TASK [Use Case 1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [lnildt01]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [lnildt01] =\> {  
“shell\_result”: {  
“changed”: true,  
“cmd”: “grep "log" /etc/fstab”,  
“delta”: “0:00:00.003975”,  
“end”: “2016-11-17 09:35:15.918729”,  
“rc”: 0,  
“start”: “2016-11-17 09:35:15.914754”,  
“stderr”: “”,  
“stdout”: “/dev/mapper/rootvg-lv\_logs /app/logs \text4 defaults 1 2”,  
“stdout\_lines”: [  
“/dev/mapper/rootvg-lv\_logs /app/logs \text4 defaults 1 2”  
],  
“warnings”:   
}  
}

TASK [Use Case 2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [lnildt01]: FAILED! =\> {“changed”: true, “cmd”: “grep "xxx" /etc/fstab”, “delta”: “0:00:00.003907”, “end”: “2016-11-17 09:35:16.234674”, “failed”: true, “rc”: 1, “start”: “2016-11-17 09:35:16.230767”, “stderr”: “”, “stdout”: “”, “stdout\_lines”: , “warnings”: }  
…ignoring

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [lnildt01] =\> {  
“shell\_result”: {  
“changed”: true,  
“cmd”: “grep "xxx" /etc/fstab”,  
“delta”: “0:00:00.003907”,  
“end”: “2016-11-17 09:35:16.234674”,  
“failed”: true,  
“rc”: 1,  
“start”: “2016-11-17 09:35:16.230767”,  
“stderr”: “”,  
“stdout”: “”,  
“stdout\_lines”: ,  
“warnings”:   
}  
}

TASK [Use Case 3] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [lnildt01]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [lnildt01] =\> {  
“shell\_result”: {  
“changed”: true,  
“cmd”: “grep "log" /etc/fstab \> /tmp/test\_ansible/use\_case\_3\_result.txt”,  
“delta”: “0:00:00.004247”,  
“end”: “2016-11-17 09:35:16.547488”,  
“rc”: 0,  
“start”: “2016-11-17 09:35:16.543241”,  
“stderr”: “”,  
“stdout”: “”,  
“stdout\_lines”: ,  
“warnings”:   
}  
}

TASK [Use Case 4] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [lnildt01]: FAILED! =\> {“changed”: true, “cmd”: “grep "xxx" /etc/fstab \> /tmp/test\_ansible/use\_case\_4\_result.txt”, “delta”: “0:00:00.004442”, “end”: “2016-11-17 09:35:16.859715”, “failed”: true, “rc”: 1, “start”: “2016-11-17 09:35:16.855273”, “stderr”: “”, “stdout”: “”, “stdout\_lines”: , “warnings”: }  
…ignoring

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [lnildt01] =\> {  
“shell\_result”: {  
“changed”: true,  
“cmd”: “grep "xxx" /etc/fstab \> /tmp/test\_ansible/use\_case\_4\_result.txt”,  
“delta”: “0:00:00.004442”,  
“end”: “2016-11-17 09:35:16.859715”,  
“failed”: true,  
“rc”: 1,  
“start”: “2016-11-17 09:35:16.855273”,  
“stderr”: “”,  
“stdout”: “”,  
“stdout\_lines”: ,  
“warnings”:   
}  
}

The tasks “Use Case 2” and “Use Case 4” failed while the shell command was executed correctly.

Moreover, in the use case 4, the file /tmp/test\_ansible/use\_case\_4\_result.txt was created: the shell command has therefore worked well.

NB1: This behavior is identical with the win\_shell module for windows hosts.

NB2 : I can’t downgrade the version of Ansible to test.

Regards,

Fabrice Perko

---

<div class="post-metadata">

**Author:** ![Matt\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matt\_Davis](https://forum.ansible.com/u/Matt_Davis)\
**Post date:** [November 17, 2016, 4:25pm UTC](https://forum.ansible.com/t/ansible-2-2-0-module-shell-win-shell-possible-issue-when-output-is-empty/23048/2 "2016-11-17T16:25:35Z")

</div>

Nope, not a bug. The shell/command modules by default consider a nonzero return code a failure. Grep returns 1 if it didn’t find anything (see “rc=1” in the response?).

You can alter this behavior using a combination of register and failed\_when to specify your own conditions for failure on the task(s) in question.

-Matt

---

<div class="post-metadata">

**Author:** ![fabrice.perko](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@fabrice.perko](https://forum.ansible.com/u/fabrice.perko)\
**Post date:** [November 17, 2016, 5:30pm UTC](https://forum.ansible.com/t/ansible-2-2-0-module-shell-win-shell-possible-issue-when-output-is-empty/23048/3 "2016-11-17T17:30:17Z")

</div>

Yes with the _failed\_when_ !!

Of course. I’ve missed it (read it some months ago but never used).

Thank you very much Matt, I will test that tomorrow.

Regards,

Fabrice
