我正在尝试使用 ansible 检查特定程序的输出是否设置为某个值。该值包含一个冒号和一个空格,无论我如何引用它,这似乎都会被记录为语法错误。
例子:
---
- hosts: all
tasks:
- raw: echo "something: else"
register: progOutput
- debug:
msg: "something else happened!"
when: progOutput.stdout_lines[-1] != "something: else"
当我运行这个程序时,第一个“raw”命令出现错误:
ERROR! Syntax Error while loading YAML.
The error appears to have been in '<snip>/test.yml': line 4, column 27, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- raw: echo "something: else"
^ here
(当然,我的实际用例涉及一个真实程序,其输出中有一个冒号,而不是“raw:echo”。但这仍然是我看到的错误。)
显然,引用有问题的字符串并不能解决问题。我还尝试用反斜杠 ( \
) 转义 :。
答案1
Ansible 的文档中记录了这。
你可以像这样逃避冒号 -
- raw: echo "something {{':'}} else"
其输出如下 -
changed: [localhost] => {
"changed": true,
"rc": 0,
"stderr": "",
"stdout": "something : else\n",
"stdout_lines": [
"something : else"
]
}
答案2
玩弄引用,我终于得到了一个有用的错误消息。显然,除非你引用整行。
以下是实际示例:
---
- hosts: localhost
tasks:
- raw: "echo 'something: else'"
register: progOutput
- debug:
msg: "something else happened!"
when: 'progOutput.stdout_lines[-1] != "something: else"'
以下是有用的错误消息:
ERROR! Syntax Error while loading YAML.
The error appears to have been in '<snip>/test.yml': line 4, column 28, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- raw: "echo 'something\: else'"
^ here
This one looks easy to fix. There seems to be an extra unquoted colon in the line
and this is confusing the parser. It was only expecting to find one free
colon. The solution is just add some quotes around the colon, or quote the
entire line after the first colon.
For instance, if the original line was:
copy: src=file.txt dest=/path/filename:with_colon.txt
It can be written as:
copy: src=file.txt dest='/path/filename:with_colon.txt'
Or:
copy: 'src=file.txt dest=/path/filename:with_colon.txt'