大网数据广告活动
服务器运维问答

quoting colon in ansible

· 52CE 我爱测

I am trying to use ansible to check that the output of a particular program is set to a certain value. That value includes a colon followed by a space, and this seems to register as a syntax error no matter how I quote it. exampl…

问题原文

原帖链接 · stochastic · CC BY-SA 3.0

I am trying to use ansible to check that the output of a particular program is set to a certain value. That value includes a colon followed by a space, and this seems to register as a syntax error no matter how I quote it.

example:

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

- debug:
    msg: "something else happened!"
  when: progOutput.stdout_lines[-1] != "something: else"

When I run this, I get an error on the first 'raw' command:

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

(naturally, my actual use case involved a real program that had a colon in its output, not 'raw: echo'. This was nevertheless the error I saw.)

Clearly, quoting the string in question doesn't fix the problem. I also tried escaping the : with a backslash (\).

社区高分解答

原帖链接 · Abhijeet Kasurde · CC BY-SA 3.0

It is documented in Ansible's documentation about this.

You can escape colon like this -

- raw: echo "something {{':'}} else"

and output of this is like -

changed: [localhost] => {
    "changed": true,
    "rc": 0,
    "stderr": "",
    "stdout": "something : else\n",
    "stdout_lines": [
        "something : else"
    ]
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。