# shell comand fails if returned value is 0

**URL:** <https://forum.ansible.com/t/shell-comand-fails-if-returned-value-is-0/14460>\
**Category:** Ansible Project\
**Created:** [January 30, 2014, 10:02am UTC](https://forum.ansible.com/t/shell-comand-fails-if-returned-value-is-0/14460 "2014-01-30T10:02:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manuel\_Estevao](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@Manuel\_Estevao](https://forum.ansible.com/u/Manuel_Estevao)\
**Post date:** [January 30, 2014, 10:02am UTC](https://forum.ansible.com/t/shell-comand-fails-if-returned-value-is-0/14460/1 "2014-01-30T10:02:41Z")

</div>

I’m using ansible 1.4.3 to install and update some RPM packages and I recently hit this bug. I’m using a pre\_task on my playbook to check if there are any updates available for a specific package, like this:

- name: check if there are updates available  
shell: yum list updates | grep “my-package” -c

register: num\_updates  
ignore\_errors: true

- debug: var=num\_updates

The -c parameter on grep returns the number of lines in which “my-package” appears. If the number of lines is 0, then there are no updates available and I can skip a few steps down the road. The thing is the task fails with this output:

failed: [IP] =\> {“changed”: true, “cmd”: "yum list updates | grep "my-package" -c ", “delta”: “0:00:20.941810”, “end”: “2014-01-30 09:50:59.964388”, “rc”: 1, “start”: “2014-01-30 09:50:39.022578”}  
stdout: 0

ok: [IP] =\> {  
“num\_updates”: {  
“changed”: true,  
“cmd”: "yum list updates | grep "my-package" -c ",  
“delta”: “0:00:20.941810”,  
“end”: “2014-01-30 09:50:59.964388”,  
“invocation”: {  
“module\_args”: “yum list updates | grep "my-package" -c”,  
“module\_name”: “shell”  
},  
“rc”: 1,  
“start”: “2014-01-30 09:50:39.022578”,  
“stderr”: “”,  
“stdout”: “0”,  
“stdout\_lines”: [  
“0”  
]  
}  
}

unless I add something like ‘_| sed “s/^0$/‘0’/g”_’ to the shell command, so that stdout turns into a string if the result is 0 and the output changes to:

changed: [IP] =\> {“changed”: true, “cmd”: "yum list updates | grep "my-package" -c | sed "s/^0$/‘0’/g" ", “delta”: “0:00:05.715204”, “end”: “2014-01-30 10:00:31.365116”, “rc”: 0, “start”: “2014-01-30 10:00:25.649912”, “stderr”: “”, “stdout”: “‘0’”}

ok: [IP] =\> {  
“num\_updates”: {  
“changed”: true,  
“cmd”: "yum list updates | grep "my-package" -c | sed "s/^0$/‘0’/g" ",  
“delta”: “0:00:05.715204”,  
“end”: “2014-01-30 10:00:31.365116”,  
“invocation”: {  
“module\_args”: “yum list updates | grep "my-package" -c | sed "s/^0$/‘0’/g"”,  
“module\_name”: “shell”  
},  
“rc”: 0,  
“start”: “2014-01-30 10:00:25.649912”,  
“stderr”: “”,  
“stdout”: “‘0’”,  
“stdout\_lines”: [  
“‘0’”  
]  
}  
}

Should I be doing something different?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 30, 2014, 2:29pm UTC](https://forum.ansible.com/t/shell-comand-fails-if-returned-value-is-0/14460/2 "2014-01-30T14:29:07Z")

</div>

You can control the failure state like this:

- shell: foo  
register: bar  
failed\_when: bar.rc == 17

---

<div class="post-metadata">

**Author:** ![Manuel\_Estevao](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@Manuel\_Estevao](https://forum.ansible.com/u/Manuel_Estevao)\
**Post date:** [January 30, 2014, 3:58pm UTC](https://forum.ansible.com/t/shell-comand-fails-if-returned-value-is-0/14460/3 "2014-01-30T15:58:23Z")

</div>

Thank you, I hadn’t noticed you could control failure states like that.
