是否有任何需要引用的有效 Ansible 布尔表达式?
Is there any valid Ansible boolean expression, which would need to be quoted?
一些 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
是否需要引用这些表达式?
是的。从手册中拿这个例子:
- 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"
一些 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
是否需要引用这些表达式?
是的。从手册中拿这个例子:
- 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"