【发布时间】: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 }}"
【问题讨论】: