是否有任何有效的Ansible布尔表达式,需要将其引用?

时间:2018-10-15 11:41:43

标签: ansible language-lawyer quotes

一些Ansible任务参数接受复杂的布尔表达式:

- name: Fail task when both files are identical
  raw: diff foo/file1 bar/file2
  register: diff_cmd
  failed_when: diff_cmd.rc == 0 or diff_cmd.rc >= 2

是否有必要引用这些表达式?

1 个答案:

答案 0 :(得分:0)

是的。从手册中获取以下示例:

- name: Fail task when the command error output prints FAILED
  command: /usr/bin/example-command -x -y -z
  register: command_result
  failed_when: "'FAILED' in command_result.stderr"

删除双引号会引发以下错误:

ERROR! Syntax Error while loading YAML.
  did not find expected key

The error appears to have been in '/home/alan-sysop/ansible/roles/test/tasks/main.yml': line 4, column 23, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      register: command_result
      failed_when: 'FAILED' in command_result.stderr
                            ^ here
This one looks easy to fix.  It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote.  For instance:

    when: "ok" in result.stdout

Could be written as:

   when: '"ok" in result.stdout'

Or equivalently:

   when: "'ok' in result.stdout"