# Running shell command in Ansible

**URL:** <https://forum.ansible.com/t/running-shell-command-in-ansible/22261>\
**Category:** Ansible Project\
**Created:** [August 10, 2016, 11:24am UTC](https://forum.ansible.com/t/running-shell-command-in-ansible/22261 "2016-08-10T11:24:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enjoy\_The\_Silence](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Enjoy\_The\_Silence](https://forum.ansible.com/u/Enjoy_The_Silence)\
**Post date:** [August 10, 2016, 11:24am UTC](https://forum.ansible.com/t/running-shell-command-in-ansible/22261/1 "2016-08-10T11:24:17Z")

</div>

Hello,  
Lately, I’ve started to use Ansible. I’m trying to write a script that pass a device as a parameter, and then chain a star sign for it.  
For example, if the device name is _/dev/sda_, i would like to count the count of line of the command: _ls -l /dev/sda\*._ I get an error after running my script.

Here is my script:  
`

- name: Get Hitachi Devices device details  
shell: lsscsi | grep HITACHI | awk ‘{print $6}’  
register: hitachiDevice

- name: Check if the volume is partitioned  
shell: ls -l “{{ hitachiDevice.rc }}”\* | wc -l  
failed\_when: hitachiDevice.rc != 1

`

Here is the error:

`FAILED! => {"changed": true, "cmd": "ls -l \"0\"* | wc -l", "delta": "0:00:00.027338", "end": "2016-08-10 14:15:12.200415", "failed": true, "failed_when_result": true, "rc": 0, "start": "2016-08-10 14:15:12.173077", "stderr": "ls: cannot access 0*: No such file or directory", "stdout": "0", "stdout_lines": ["0"], "warnings": []}`

Can someone tell my what is the issue, and how can i fix it?

---

<div class="post-metadata">

**Author:** ![Enjoy\_The\_Silence](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Enjoy\_The\_Silence](https://forum.ansible.com/u/Enjoy_The_Silence)\
**Post date:** [August 10, 2016, 2:57pm UTC](https://forum.ansible.com/t/running-shell-command-in-ansible/22261/2 "2016-08-10T14:57:52Z")

</div>

I’ve made some changes in the script. I still need some help with the script.

Here is the updated code.

`- name: Get Hitachi Devices device details`  
`shell: lsscsi | grep HITACHI | awk '{print $6}'`  
`register: hitachiDevice`

`- name: Check if the volume is partitioned`  
`shell: ls -l "{{ hitachiDevice.stdout }}"* | wc -l`  
`failed_when: hitachiDevice.rc != 1`

Here is the error:

`FAILED! => `{“changed”: true, “cmd”: “ls -l "/dev/sdg"\* | wc -l”, “delta”: “0:00:00.010330”, “end”: “2016-08-10 17:35:34.819617”, “failed”: true, “failed\_when\_result”: true, “rc”: 0, “start”: “2016-08-10 17:35:34.809287”, “stderr”: “”, “stdout”: “1”, “stdout\_lines”: [“1”], “warnings”: }

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 10, 2016, 3:47pm UTC](https://forum.ansible.com/t/running-shell-command-in-ansible/22261/3 "2016-08-10T15:47:09Z")

</div>

As the message say "failed\_when\_result": true, so you failed\_when triggered. rc = 0 is OK and rc \> 0 is an ERROR.

So for you failed\_when to make sense it should be != 0.

I also recommend moving the failed\_when to the "Get Hitachi Devices device details" task, since it is that task that fails it will be more clear and you debugging would be easier.
