Running shell command in Ansible

327 views
Skip to first unread message

Enjoy The Silence

unread,
Aug 10, 2016, 9:10:22 AM8/10/16
to Ansible Project
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?

Enjoy The Silence

unread,
Aug 10, 2016, 10:57:52 AM8/10/16
to Ansible Project
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 -"{{ 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": []}

Kai Stian Olstad

unread,
Aug 10, 2016, 11:47:21 AM8/10/16
to ansible...@googlegroups.com
On 10. aug. 2016 16:57, Enjoy The Silence wrote:
> 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": []}

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.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages