Ansible中的YAML语法问题

时间:2016-09-06 14:56:41

标签: shell yaml ansible

我在Ansible脚本中有以下行:

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

运行脚本时出现以下错误:

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

有谁能告诉我如何解决这个语法问题?

2 个答案:

答案 0 :(得分:3)

你需要逃避内部双引号。

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

如果您的字符串中没有冒号,您可以只删除外部引号,但这会引发另一个YAML错误。不过,这是一个选项,但你需要解决这样的冒号问题:

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

可以找到此冒号“转义”的说明hereherehere

答案 1 :(得分:1)

你需要逃避里面的双引号:

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