From 20b84fc709989b8939a4ca88863e7fc0b4c4c371 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 1 Nov 2022 02:33:43 -0400 Subject: [PATCH] linnode inventory, remove redundant (#5438) * remove redundant templar is already in base class env var is already consulted in via config resolution * more whites * no need to import templar again * Add changelog fragment. * Try to update tests. Co-authored-by: Felix Fontein --- changelogs/fragments/5438-linode.yml | 2 ++ plugins/inventory/linode.py | 15 +++------------ tests/unit/plugins/inventory/test_linode.py | 5 ++++- 3 files changed, 9 insertions(+), 13 deletions(-) create mode 100644 changelogs/fragments/5438-linode.yml diff --git a/changelogs/fragments/5438-linode.yml b/changelogs/fragments/5438-linode.yml new file mode 100644 index 0000000000..ce2aeadcf3 --- /dev/null +++ b/changelogs/fragments/5438-linode.yml @@ -0,0 +1,2 @@ +minor_changes: + - "linode inventory plugin - simplify option handling (https://github.com/ansible-collections/community.general/pull/5438)." diff --git a/plugins/inventory/linode.py b/plugins/inventory/linode.py index 8790da7079..ea87a9a58e 100644 --- a/plugins/inventory/linode.py +++ b/plugins/inventory/linode.py @@ -126,7 +126,6 @@ import os from ansible.errors import AnsibleError, AnsibleParserError from ansible.module_utils.six import string_types from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable -from ansible.template import Templar try: @@ -145,22 +144,14 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): 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: - access_token = os.environ['LINODE_ACCESS_TOKEN'] - except KeyError: - pass + if self.templar.is_template(access_token): + access_token = self.templar.template(variable=access_token, disable_lookups=False) if access_token is None: raise AnsibleError(( 'Could not retrieve Linode access token ' - 'from plugin configuration or environment' + 'from plugin configuration sources' )) self.client = LinodeClient(access_token) diff --git a/tests/unit/plugins/inventory/test_linode.py b/tests/unit/plugins/inventory/test_linode.py index 60b3c8cb68..4e6a018850 100644 --- a/tests/unit/plugins/inventory/test_linode.py +++ b/tests/unit/plugins/inventory/test_linode.py @@ -18,12 +18,15 @@ mandatory_py_version = pytest.mark.skipif( from ansible.errors import AnsibleError, AnsibleParserError from ansible.parsing.dataloader import DataLoader +from ansible.template import Templar from ansible_collections.community.general.plugins.inventory.linode import InventoryModule @pytest.fixture(scope="module") def inventory(): - return InventoryModule() + plugin = InventoryModule() + plugin.templar = Templar(loader=DataLoader()) + return plugin def test_missing_access_token_lookup(inventory):