我正在尝试使用ansible来检查特定程序的输出是否设置为某个值。 该值包括一个冒号,后面跟一个空格,这似乎注册为一个语法错误,不pipe我怎么引用它。
例:
--- - hosts: all 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 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
(当然,我的实际使用案例涉及到一个真正的程序,在其输出中有一个冒号,而不是“原始的:回声”,但这是我看到的错误。
显然,引用有问题的string不能解决问题。 我也试着用反斜杠( \ )来转义:
玩弄引用,我终于得到了一个有用的错误信息。 显然,除非引用整行,否则会混淆YAMLparsing器。
这是一个工作的例子:
--- - 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'