Ansible Playbook msg var插值似乎只适用于carrot>;在yml文件中

Ansible Playbook msg var插值似乎只适用于carrot>;在yml文件中,ansible,yaml,Ansible,Yaml,发现一种我无法解释的奇怪行为 运行一个小型Ansible Playbook来收集和显示来自主机的操作系统信息(例如:“Debian GNU/Linux 10”) 我随机发现了这个解决方案(使用carrot>),但找不到解释为什么它是这样工作的 --- - name: Show Operating System version information hosts: all gather_subset: distribution_version tasks:

发现一种我无法解释的奇怪行为

运行一个小型Ansible Playbook来收集和显示来自主机的操作系统信息(例如:“Debian GNU/Linux 10”)

我随机发现了这个解决方案(使用carrot>),但找不到解释为什么它是这样工作的

---
  - name: Show Operating System version information
    hosts: all
    gather_subset: distribution_version
    tasks:

      - name: Display operating system facts
        debug: 
          msg: >
            {{ ansible_facts.lsb.description }}
产生成功的输出

ok: [orville.lan] => {
    "msg": "Ubuntu 19.10"
}
但如果更改为此(删除>)

然后运行playbook生成该错误序列

ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: No JSON object could be decoded

Syntax Error while loading YAML.
  found unacceptable key (unhashable type: 'AnsibleMapping')

The error appears to be in '/home/eschin/Repositories/ansible-files/OperatingSystemReport.yml': line 9, column 17, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

        debug:
          msg: {{ ansible_facts.lsb.description }}
                ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
                                                                                                                                      with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"
我知道carrot>将所有后续缩进行视为一行,即将换行符转换为空格,但还有什么“秘密酱汁”使前者起作用,而不是后者

解决方案:

只需将插值设置为字符串(carrot
为您所做的),第二个示例就可以工作。我想我已经试过了,但肯定没用。。。但我又试了一次,现在终于成功了。对IMP进行编码一定让我很烦恼(0:)

所以这是可行的

---
  - name: Show Operating System version information
    hosts: all
    gather_subset: distribution_version
    tasks:

      - name: Display operating system facts
        debug:
          msg: "{{ ansible_facts.lsb.description }}"

{{}}
用于jinja2模板,要使其工作,您需要将模板放在引号
”或“
中的字符串中。因此,下面应该可以工作:

msg:“{{ansible_facts.lsb.description}”
#或者,在没有引号的字符串中(最好使用引号,因为某些特殊字符的yaml处理器实现可能不同),
msg:描述是{{ansible_facts.lsb.description}}
现在呢,

我随机发现了这个解决方案(使用carrot>),但不能 找到它工作的原因

|
分别表示yaml中的
折叠的
文字的
。由于
已经将模板放入字符串中,因此它按照您在问题中指出的方式工作


看看basic。

是的,这很有道理……这太让人恼火了……我肯定我试过用引号把它括起来(使它成为字符串),但还是失败了……但我刚刚试过,它成功了:)一定是犯了一个“累人的错误”,我没有注意到。谢谢!
---
  - name: Show Operating System version information
    hosts: all
    gather_subset: distribution_version
    tasks:

      - name: Display operating system facts
        debug:
          msg: "{{ ansible_facts.lsb.description }}"