【发布时间】:2020-07-07 13:15:49
【问题描述】:
我正在尝试使用 Ansible 实现以下目标:
定义一个 YAML 文件,其中包含一些点格式变量(variables.yml)
database.hosts[0]: "db0"
database.hosts[1]: "db1"
database.hosts[2]: "db2"
foo.bar: 1
foo.baz: 2
使用我的剧本 (playbook.yml) 中的 include_vars 模块加载 variables.yml 中的变量并以树结构打印它们
- hosts: all
gather_facts: not
tasks:
- name: "Loading vars"
run_once: true
include_vars:
file: 'variables.yml'
- name: "Testing"
debug:
msg: "{{ foo }}"
- name: "Testing"
debug:
msg: "{{ database }}"
运行此程序会导致以下错误:
fatal: [host0]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'foo' is undefined\n\nThe error appears to be in '.../playbook.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: \"Testing\"\n ^ here\n"}
这清楚地表明,YAML 文件中的每个属性都已作为单独的属性加载,而不是作为植根于 database 和 foo 的两棵树中的属性。 p>
当然,如果我按以下方式指定属性,剧本会按预期工作:
database:
hosts:
- "db0"
- "db1"
- "db2"
foo:
bar: 1
baz: 2
但是,我需要 YAML 变量文件采用点分格式,而不是经典的缩进格式。有什么办法可以做到这一点?例如:与 include_vars 不同的模块或我可以添加到 ansible.cfg 文件中的某些配置?我已经尝试过使用 hash_behaviour=merge,但这没有帮助。
【问题讨论】:
标签: variables ansible format yaml