【问题标题】:Use dotted YAML variables file in Ansible在 Ansible 中使用虚线 YAML 变量文件
【发布时间】: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 文件中的每个属性都已作为单独的属性加载,而不是作为植根于 databasefoo 的两棵树中的属性。 p>

当然,如果我按以下方式指定属性,剧本会按预期工作:

database:
  hosts:
    - "db0"
    - "db1"
    - "db2"        

foo:
  bar: 1
  baz: 2

但是,我需要 YAML 变量文件采用点分格式,而不是经典的缩进格式。有什么办法可以做到这一点?例如:与 include_vars 不同的模块或我可以添加到 ansible.cfg 文件中的某些配置?我已经尝试过使用 hash_behaviour=merge,但这没有帮助。

【问题讨论】:

    标签: variables ansible format yaml


    【解决方案1】:

    问:“我需要 YAML 变量文件采用点分格式,而不是经典的缩进格式。有什么方法可以实现吗?”

    A:不,这是不可能的。见Creating valid variable names

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 1970-01-01
      • 2015-03-29
      • 2021-12-07
      • 1970-01-01
      • 2022-01-23
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多