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

linode: Allow templating token for dynamic inventory #4040

Merged
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
2 changes: 2 additions & 0 deletions changelogs/fragments/4040-linode-token-templating.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- linode inventory plugin - allow templating of ``access_token`` variable in Linode inventory plugin (https://github.com/ansible-collections/community.general/pull/4040).
15 changes: 13 additions & 2 deletions plugins/inventory/linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
# Minimal example. `LINODE_ACCESS_TOKEN` is exposed in environment.
plugin: community.general.linode

# You can use Jinja to template the access token.
plugin: community.general.linode
access_token: "{{ lookup('ini', 'token', section='your_username', file='~/.config/linode-cli') }}"
# For older Ansible versions, you need to write this as:
# access_token: "{{ lookup('ini', 'token section=your_username file=~/.config/linode-cli') }}"

# Example with regions, types, groups and access token
plugin: community.general.linode
access_token: foobar
Expand Down Expand Up @@ -105,6 +111,7 @@
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.module_utils.six import string_types
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
from ansible.template import Templar


try:
Expand All @@ -119,10 +126,14 @@ class InventoryModule(BaseInventoryPlugin, Constructable):

NAME = 'community.general.linode'

def _build_client(self):
def _build_client(self, loader):
"""Build the Linode client."""

t = Templar(loader=loader)

access_token = self.get_option('access_token')
if t.is_template(access_token):
access_token = t.template(variable=access_token, disable_lookups=False)

if access_token is None:
try:
Expand Down Expand Up @@ -287,7 +298,7 @@ def parse(self, inventory, loader, path, cache=True):
raise AnsibleError('the Linode dynamic inventory plugin requires linode_api4.')

config_data = self._read_config_data(path)
self._build_client()
self._build_client(loader)

self._get_instances_inventory()

Expand Down
6 changes: 4 additions & 2 deletions tests/unit/plugins/inventory/test_linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


from ansible.errors import AnsibleError, AnsibleParserError
from ansible.parsing.dataloader import DataLoader
from ansible_collections.community.general.plugins.inventory.linode import InventoryModule


Expand All @@ -39,10 +40,11 @@ def inventory():
return InventoryModule()


def test_access_token_lookup(inventory):
def test_missing_access_token_lookup(inventory):
loader = DataLoader()
inventory._options = {'access_token': None}
with pytest.raises(AnsibleError) as error_message:
inventory._build_client()
inventory._build_client(loader)
assert 'Could not retrieve Linode access token' in error_message


Expand Down