【问题标题】:Ansible Playbook msg var interpolation seems to only work with carrot > in yml fileAnsible Playbook msg var 插值似乎仅适用于 yml 文件中的胡萝卜 >
【发布时间】:2020-07-17 23:17:35
【问题描述】:

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

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

我偶然发现了这个解决方案(使用胡萝卜>),但找不到解释为什么它会这样工作;

---
  - 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"
}

但是如果改成这个(去掉>);

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

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

然后运行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 }}"

我知道胡萝卜 > 将所有后面的缩进行视为一行,即将换行符转换为空格,但是还有什么其他“秘诀”可以使前者工作,而不是后者?

解决方案:

只需将插值设为字符串,胡萝卜> 为您执行此操作,即可使第二个示例有效。我以为我已经尝试过了,并且确定它不起作用……但又试了一次,它现在起作用了。编码小鬼一定是在惹我 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 }}"

【问题讨论】:

    标签: ansible yaml


    【解决方案1】:

    {{}} 用于 jinja2 模板,要使其正常工作,您需要将模板放在引号 ' or " 内的字符串中。所以下面应该工作:

    msg: "{{ ansible_facts.lsb.description }}"
    
    # Or, within a string without quote like below (better to use quote because the yaml processor implementation may differ for some special characters),
    
    msg: The description is {{ ansible_facts.lsb.description }}
    

    现在,

    我偶然发现了这个解决方案(使用胡萝卜>),但不能 找到它为什么起作用的解释

    >| 在 yaml 中分别代表 foldedliteral multiline strings。由于> 已经将模板放入字符串中,因此它可以按照您在问题中指出的那样工作。

    也看看基本的YAML syntax

    【讨论】:

    • 是的,这很有道理...这太疯狂了...我确定我尝试将其放在引号中(使其成为字符串),但仍然失败...但我只是尝试过,并且它起作用了:) 一定犯了一个我没有注意到的“令人厌烦的错误”。谢谢!
    猜你喜欢
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    相关资源
    最近更新 更多