【问题标题】:Fail to use yaml reference in ansible inventory plugin无法在 ansible 库存插件中使用 yaml 引用
【发布时间】:2020-04-21 18:42:05
【问题描述】:

我想将此配置与库存插件一起使用

# test_inventory_xxx.yml
plugin: cloudscale # or openstack or ...
inventory_hostname: &inventory_hostname_value uuid

compose:
  setting_of_inventory_hostname: *inventory_hostname_value

我没有收到错误,但未设置该值。它是有效的 yaml。 (至少我的检查员和我自己都看到了错误。

所以我决定使用标准的构造插件来简化它:

# inventory_constructed.yaml
plugin: constructed

# add variables to existing inventory

keyed_groups:
  - key: from_inventory
    prefix: inventory
    parent_group: &common_parent_group test_group_1

compose:
  var_from_constructed: 1233456789
  also_from_constr: "'also'" # must be in quotes 2x!
  some_from_constr: &ref1 1234567777
  ref_from_constr: *ref1 # this works fine
  ref_to_test: *common_parent_group # <--- this line returns an error

strict: yes

现在我收到错误消息:Could not set ref_to_test for host my_host: 'test_group_1' is undefined
但是当我取消注释标记的行时它通过了。 (引用 &common_parent_group 仍然被定义,但没有与 *common_parent_group 一起使用。)为什么 test_group_1 在一种情况下未定义,而在另一种情况下没有?

如何复现:ansible -i some_of/your_inventory -i inventory_constructed.yaml -m debug -a var=vars

我做错了什么?或者还有什么问题?

(我认为这是一个缺失的功能,所以https://github.com/ansible/ansible/issues/69043中的原始信息)

【问题讨论】:

    标签: ansible yaml ansible-inventory


    【解决方案1】:

    似乎parent_group 采用文字字符串,而ref_to_test 采用Jinja2 表达式(因为它在compose 下)。如果你写它应该以同样的方式失败

      ref_to_test: test_group_1
    

    因为test_group_1 根本不是 Jinja2 变量。你必须写

      ref_to_test: "'test_group_1'"
    

    就像上面一样,Jinja2 看到 'test_group_1' 这是一个文字字符串。这也意味着您不能使用别名,因为 parent_group 不会使用 Jinja2 评估其内容,因此不应在其内容中包含引号。

    【讨论】:

    • 好的,谢谢。我懂了。所以它在构造的插件中不起作用。第一个示例 cloudscale 插件是什么?
    • 啊,我明白了,其他库存插件也一样。
    猜你喜欢
    • 2019-06-26
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多