在ansible中引用冒号

sto*_*tic 7 ansible yaml

我正在尝试使用 ansible 来检查特定程序的输出是否设置为某个值。该值包括一个冒号,后跟一个空格,无论我如何引用它,这似乎都会注册为语法错误。

例子:

---
- hosts: all
  tasks:
    - raw: echo "something: else"
  register: progOutput

- debug:
    msg: "something else happened!"
  when: progOutput.stdout_lines[-1] != "something: else"
Run Code Online (Sandbox Code Playgroud)

当我运行它时,第一个“原始”命令出现错误:

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
Run Code Online (Sandbox Code Playgroud)

(自然,我的实际用例涉及一个真实的程序,它的输出中有一个冒号,而不是 'raw: echo'。然而,这仍然是我看到的错误。)

显然,引用有问题的字符串并不能解决问题。我还尝试使用反斜杠 ( \)转义 : 。

sto*_*tic 7

玩转引用,最后我收到了一条有用的错误消息。显然,除非您引用整行,否则您会混淆 YAML 解析器。

这是工作示例:

---
- hosts: localhost
  tasks:
    - raw: "echo 'something: else'"
      register: progOutput

    - debug:
        msg: "something else happened!"
      when: 'progOutput.stdout_lines[-1] != "something: else"'
Run Code Online (Sandbox Code Playgroud)

这是有用的错误消息:

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'
Run Code Online (Sandbox Code Playgroud)


Abh*_*rde 6

它记录在 Ansible 的关于this的文档中。

你可以像这样逃避冒号——

- raw: echo "something {{':'}} else"
Run Code Online (Sandbox Code Playgroud)

这个输出就像 -

changed: [localhost] => {
    "changed": true,
    "rc": 0,
    "stderr": "",
    "stdout": "something : else\n",
    "stdout_lines": [
        "something : else"
    ]
}
Run Code Online (Sandbox Code Playgroud)