Omri Omri - 1 year ago 139
YAML Question

Chaining a star sign in Ansible playbook

Lately, I've started to use Ansible, and i need some help with a playbook.
I'm trying to get a device details as a parameter, and then chain a star sign for it, in order to run a shell command. For example, if the device is


, the shell command will be
ls -l /dev/sda*

- 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 i get after running the script:

"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": [
"warnings": []

Any one know what is the issue and how can i fix it?

Answer Source

hitachiDevice.rc contains result code of previous command.
I bet it is always 0 if grep is successful.
So you next command will almost always will be ls -l 0* | wc -l which gives you the error

ls: cannot access 0*: No such file or directory

I think you need something like hitachiDevice.stdout, depending on what you want to achieve.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download