Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: inventory plugins support yaml references in "compose" #69043

Closed
SimonHeimberg opened this issue Apr 20, 2020 · 7 comments
Closed

Comments

@SimonHeimberg
Copy link
Contributor

SimonHeimberg commented Apr 20, 2020

SUMMARY

It would be handy if inventory plugins would support yaml references in compose (for settings with string values). This would allow to pass on settings to variables (see ADDITIONAL INFORMATION for an example):

ISSUE TYPE
  • Feature Idea
COMPONENT NAME

inventory plugin

ADDITIONAL INFORMATION

example inventory showing how settings could be passed to variables:

inventory/yyy.yaml

plugin: openstack # or clouldscale or ...
some_plugin_setting: &ref1 12345
inventory_hostname: &inventory_hostname_value uuid

compose:
  setting_1: *ref1 # should work already now
  setting_of_inventory_hostname: *inventory_hostname_value # fails now, is interpreted as variable name

This does not work because compose expects jinja2 expressions. When strings are passed, they are interpreted as variables. And I see no way to put quotes around *inventory_hostname_value or tell compose that the value is a string.

suggestion

Feature request: Please provide a way to use (string) setting values as variables.

  • by allowing something like this
    compose:
        some_setting1:
            compose_setting:
                value: *some_string_ref
                type: value
  • by defining another section:
    compose_values: #similar as compose, but takes values instead of jinja2
        some_setting1: *some_string_ref

old info

I do not get an error (because strict is not set), but I do not see the variables settings_1 and setting_of_inventory_hostname.

Currently I can copy the values manually, which is error prone.

ansible version: 2.9.4
yaml syntax for saml references: https://en.wikipedia.org/wiki/YAML#Syntax or https://yaml.org/spec/1.0/#id2489959

@sivel
Copy link
Member

sivel commented Apr 20, 2020

This should already be possible. What happens when you do this?

For reference, your syntax is backwards. It should be &ref1 instead of ref1&, and *ref1 instead of ref1*.

@SimonHeimberg
Copy link
Contributor Author

Oops, true. Thanks for the hint. Somewhen during testing I did the syntax wrong. I fixed and extended the question.
So I have to redo the tests. An update will follow.

@SimonHeimberg
Copy link
Contributor Author

Why does this fail?

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

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.)

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

@bcoca
Copy link
Member

bcoca commented Apr 21, 2020

List Information

Hi!

Thanks very much for your interest in Ansible. It sincerely means a lot to us.

This appears to be a user question, and we'd like to direct these kinds of things to either the mailing list or the IRC channel.

If you can stop by there, we'd appreciate it. This allows us to keep the issue tracker for bugs, pull requests, RFEs and the like.

Thank you once again and we look forward to seeing you on the list or IRC. Thanks!

@bcoca bcoca closed this as completed Apr 21, 2020
@SimonHeimberg
Copy link
Contributor Author

OK, fine. I will come back here in case it is an issue.

@SimonHeimberg SimonHeimberg changed the title inventory plugins support yaml references feature request: inventory plugins support yaml references in "compose" Apr 22, 2020
@SimonHeimberg
Copy link
Contributor Author

SimonHeimberg commented Apr 22, 2020

OK, I have the whole problem now:
references in compose: do not work as I expected, because compose expects values which are Jinja2 expressions

compse:
    exInv: "string_value" # is invalid
    evVal: "'string_value'" # is valid
    # so this means
    refInt: &ref_to_integer # is valid, because an integer is valid jinja2
    refStr: &ref_to_string # is invalid, because it is looked as variable name    

So I would have to put quotes around the resolved reference value, what yaml references does not support.


original source:

It seems like parent_group takes a literal string while ref_to_test takes a Jinja2 expression (because it's under compose). It should fails 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'"

from https://stackoverflow.com/questions/61350793/fail-to-use-yaml-reference-in-ansible-inventory-plugin

@SimonHeimberg
Copy link
Contributor Author

SimonHeimberg commented Apr 22, 2020

@bcoca could you please reopen the issue. It is a feature request.

I have extended the description.

@ansible ansible locked and limited conversation to collaborators May 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants