simohe
simohe

Reputation: 651

Fail to use yaml reference in ansible inventory plugin

I would like to use this config with an inventory plugin

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

compose:
  setting_of_inventory_hostname: *inventory_hostname_value

I get no error, but the value is not set. And it is valid yaml. (At least my checker nor myself see an error.

So I decided to simplify it by using the constructed plugin, which is standard:

# 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

Now I get the error: Could not set ref_to_test for host my_host: 'test_group_1' is undefined
But it passes when I uncomment the marked line. (the ref &common_parent_group is still defined, but not used with *common_parent_group.) Why is test_group_1 undefined in one case, but not in the other?

How to reproduce: ansible -i some_of/your_inventory -i inventory_constructed.yaml -m debug -a var=vars

What do I do wrong? Or what else is the problem?

(I tought it is an missing feature, so original info in https://github.com/ansible/ansible/issues/69043)

Upvotes: 0

Views: 1444

Answers (1)

flyx
flyx

Reputation: 39678

It seems like parent_group takes a literal string while ref_to_test takes a Jinja2 expression (because it's under compose). It should fail the same way if you write

  ref_to_test: test_group_1

because test_group_1 simply isn't a Jinja2 variable. You'll have to write

  ref_to_test: "'test_group_1'"

just like above so Jinja2 sees 'test_group_1' which is a literal string. This also means you can't use an alias because parent_group does not evaluate its content with Jinja2 and therefore shouldn't include quotes in its content.

Upvotes: 2

Related Questions