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

Inventory plugin: Allow token to be provided through extra-vars. #156

Merged
merged 1 commit into from
Jan 30, 2023

Conversation

n00b42
Copy link
Contributor

@n00b42 n00b42 commented Sep 30, 2022

SUMMARY

Allow token to be provided through extra-vars.
Allows "dynamic" insertion of the token without changing the environment variables

Fixes #181

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

hetzner.hcloud.hcloud

ADDITIONAL INFORMATION

Only thing I am not perfectly sure about is, if one can safely use self._compose and self._compose in a custom plugin.

@apricote
Copy link
Collaborator

Hey @n00b42, thanks for the contribution!

This fails for me with the nested extra var:

[WARNING]:  * Failed to parse ./hcloud.yaml with auto plugin: 'hetzner' is undefined

It works if I use hetzner_apitoken instead (or any other value that does not use a dot)

Copy link
Collaborator

@apricote apricote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already support templating logic when evaluating token. What is missing from that, is that the extra vars are not passed to self.templar.template. self._compose() under the hood passes those variables and then calls self.templar.template.

To avoid templating twice with nearly the same logic, what do you think about adding the self._vars to our templar instance?

self.templar.available_variables = self._vars
self.api_token = self.templar.template(self.get_option("token"), fail_on_undefined=False) or os.getenv(self.token_env)

Then you can use the (extra) vars in the normal token templating:

token: "{{ hcloud_token }}"

@apricote apricote added the enhancement New feature or request label Jan 27, 2023
@n00b42
Copy link
Contributor Author

n00b42 commented Jan 27, 2023

I am fine with that as well, just needed a way to pass it using a variable.
Should I update the PR or will you do the changes yourself?

@apricote
Copy link
Collaborator

I am fine with that as well, just needed a way to pass it using a variable. Should I update the PR or will you do the changes yourself?

It would be great if you can update the PR, but I can also do it if you have no time.

@n00b42
Copy link
Contributor Author

n00b42 commented Jan 30, 2023

changed as suggested.

@apricote apricote merged commit ccfa51b into ansible-collections:main Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inventory plugin: Allow token to be provided through extra-vars
2 participants