From ce7c0700305a747ec6959abc53bc2a357f4be02c Mon Sep 17 00:00:00 2001 From: Lena Garber Date: Thu, 29 Jun 2023 14:12:28 -0400 Subject: [PATCH] Support Jinja2 templating on the api_token field --- plugins/inventory/instance.py | 5 +++++ .../targets/instance_inventory/playbooks/teardown.yml | 3 ++- .../playbooks/test_inventory_templatetoken.yml | 11 +++++++++++ tests/integration/targets/instance_inventory/runme.sh | 4 ++++ .../templates/templatetoken.instance.yml | 8 ++++++++ 5 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/integration/targets/instance_inventory/playbooks/test_inventory_templatetoken.yml create mode 100644 tests/integration/targets/instance_inventory/templates/templatetoken.instance.yml diff --git a/plugins/inventory/instance.py b/plugins/inventory/instance.py index f5291f2a..d523c9e2 100644 --- a/plugins/inventory/instance.py +++ b/plugins/inventory/instance.py @@ -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( ( diff --git a/tests/integration/targets/instance_inventory/playbooks/teardown.yml b/tests/integration/targets/instance_inventory/playbooks/teardown.yml index 8e9df2ce..006d60db 100644 --- a/tests/integration/targets/instance_inventory/playbooks/teardown.yml +++ b/tests/integration/targets/instance_inventory/playbooks/teardown.yml @@ -23,4 +23,5 @@ loop: - filter.instance.yml - nofilter.instance.yml - - keyedgroups.instance.yml \ No newline at end of file + - keyedgroups.instance.yml + - templatetoken.instance.yml \ No newline at end of file diff --git a/tests/integration/targets/instance_inventory/playbooks/test_inventory_templatetoken.yml b/tests/integration/targets/instance_inventory/playbooks/test_inventory_templatetoken.yml new file mode 100644 index 00000000..9a82316a --- /dev/null +++ b/tests/integration/targets/instance_inventory/playbooks/test_inventory_templatetoken.yml @@ -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 \ No newline at end of file diff --git a/tests/integration/targets/instance_inventory/runme.sh b/tests/integration/targets/instance_inventory/runme.sh index aab75fd6..fde50e0c 100755 --- a/tests/integration/targets/instance_inventory/runme.sh +++ b/tests/integration/targets/instance_inventory/runme.sh @@ -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 "$@" \ No newline at end of file diff --git a/tests/integration/targets/instance_inventory/templates/templatetoken.instance.yml b/tests/integration/targets/instance_inventory/templates/templatetoken.instance.yml new file mode 100644 index 00000000..2bfa3e40 --- /dev/null +++ b/tests/integration/targets/instance_inventory/templates/templatetoken.instance.yml @@ -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 \ No newline at end of file