Ansible 2.2.0 - Module shell & win_shell - possible issue when output is empty

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

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

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