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

new: Support Jinja2 templating on the api_token field #384

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugins/inventory/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ def _build_client(self) -> None:
except KeyError:
pass

if self.templar.is_template(api_token):
api_token = self.templar.template(
variable=api_token, disable_lookups=False
)

if api_token is None:
raise AnsibleError(
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
loop:
- filter.instance.yml
- nofilter.instance.yml
- keyedgroups.instance.yml
- keyedgroups.instance.yml
- templatetoken.instance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- meta: refresh_inventory

- assert:
that:
- '"ansible-test-inventory" in hostvars'
- hostvars | length == 1
4 changes: 4 additions & 0 deletions tests/integration/targets/instance_inventory/runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ ANSIBLE_INVENTORY=filter.instance.yml ansible-playbook playbooks/test_inventory_
ansible-playbook playbooks/create_inventory.yml --extra-vars "template=keyedgroups.instance.yml" "$@"
ANSIBLE_INVENTORY=keyedgroups.instance.yml ansible-playbook playbooks/test_inventory_keyedgroups.yml "$@"

# Test an inventory with a templated `api_token` field
ansible-playbook playbooks/create_inventory.yml --extra-vars "template=templatetoken.instance.yml" "$@"
ANSIBLE_INVENTORY=templatetoken.instance.yml ansible-playbook playbooks/test_inventory_templatetoken.yml "$@"

# Clean up
ansible-playbook playbooks/teardown.yml "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugin: linode.cloud.instance
api_token: '{{ "{{ \"" + api_token + "\" }}" }}' # Hacky test for templating
types:
- g6-nanode-1
tags:
- ansible-inventory-node
regions:
- us-east