无法在ansible库存插件中使用yaml引用



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

# 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
但当我取消注释标记行时,它就通过了。(ref&common_parent_group仍然被定义,但没有与*common_parend_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)

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

ref_to_test: test_group_1

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

ref_to_test: "'test_group_1'"

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

最新更新