Omri Omri - 3 months ago 22
YAML Question

YML syntax issue in Ansible

I have the following line in Ansible script:

- name: get UUID
shell: "blkid | grep test--tgroup* | grep xfs | awk -F : '{print "blkid -s UUID -o value "$1}' |sh"
register: UUID_value


I get the following error when i run the script:

ERROR! Syntax Error while loading YAML.
The error appears to have been in '/etc/ansible/config/test.yml': line...
May be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:
shell: "blkid | grep test--tgroup* | grep xfs | awk -F : '{print "blkid -s UUID -o value "$1}' |sh"
^ here


Can anyone tell me how to fix this syntax issue?

Answer

You need to escape the inner doublequotes.

- name: get UUID    
  shell: "blkid | grep test--tgroup* | grep xfs | awk -F : '{print \"blkid -s UUID -o value \"$1}' |sh"
  register: UUID_value

If there wasn't the colon in your string you would have been able to just to remove the outer quotes, but that would raise another YAML error. Still, this is an option but you need to work around the colon problem like this:

- name: get UUID    
  shell: blkid | grep test--tgroup* | grep xfs | awk -F {{ ":" }} '{print "blkid -s UUID -o value "$1}' |sh
  register: UUID_value

Explanation for this colon "escaping" can be found here or here or here.