【问题标题】:YAML syntax issue in AnsibleAnsible 中的 YAML 语法问题
【发布时间】:2017-01-14 02:16:45
【问题描述】:

我在 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

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

【问题讨论】:

标签: shell yaml ansible


【解决方案1】:

您需要转义内部双引号。

- 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找到。

【讨论】:

  • 效果很好。谢谢你的解释!
【解决方案2】:

你需要转义里面的双引号:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 2018-02-07
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多