From 75fd32ca5512e5340ec35875136a91b1d181ddf1 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Fri, 13 Nov 2020 21:34:56 +0100 Subject: [PATCH] Fix the linode inventory plugin, and the redis and memcached cache plugins (#1281, #1282, #1283). --- changelogs/fragments/fix-plugin-imports.yml | 4 ++++ plugins/cache/memcached.py | 6 +++++- plugins/cache/redis.py | 6 +++++- plugins/inventory/linode.py | 6 +++++- 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/fix-plugin-imports.yml diff --git a/changelogs/fragments/fix-plugin-imports.yml b/changelogs/fragments/fix-plugin-imports.yml new file mode 100644 index 0000000000..93afa59bb0 --- /dev/null +++ b/changelogs/fragments/fix-plugin-imports.yml @@ -0,0 +1,4 @@ +bugfixes: +- "linode inventory plugin - make sure that plugin errors out on initialization if the required library is not found, and not on load-time (https://github.com/ansible-collections/community.general/pull/1297)." +- "redis cache plugin - make sure that plugin errors out on initialization if the required library is not found, and not on load-time (https://github.com/ansible-collections/community.general/pull/1297)." +- "memcached cache plugin - make sure that plugin errors out on initialization if the required library is not found, and not on load-time (https://github.com/ansible-collections/community.general/pull/1297)." diff --git a/plugins/cache/memcached.py b/plugins/cache/memcached.py index f73dba845e..3cf670d7a0 100644 --- a/plugins/cache/memcached.py +++ b/plugins/cache/memcached.py @@ -57,8 +57,9 @@ from ansible.utils.display import Display try: import memcache + HAS_MEMCACHE = True except ImportError: - raise AnsibleError("python-memcached is required for the memcached fact cache") + HAS_MEMCACHE = False display = Display() @@ -187,6 +188,9 @@ class CacheModule(BaseCacheModule): self._timeout = C.CACHE_PLUGIN_TIMEOUT self._prefix = C.CACHE_PLUGIN_PREFIX + if not HAS_MEMCACHE: + raise AnsibleError("python-memcached is required for the memcached fact cache") + self._cache = {} self._db = ProxyClientPool(connection, debug=0) self._keys = CacheModuleKeys(self._db, self._db.get(CacheModuleKeys.PREFIX) or []) diff --git a/plugins/cache/redis.py b/plugins/cache/redis.py index caae3ae438..fe41c4c051 100644 --- a/plugins/cache/redis.py +++ b/plugins/cache/redis.py @@ -73,8 +73,9 @@ from ansible.utils.display import Display try: from redis import StrictRedis, VERSION + HAS_REDIS = True except ImportError: - raise AnsibleError("The 'redis' python module (version 2.4.5 or newer) is required for the redis fact cache, 'pip install redis'") + HAS_REDIS = False display = Display() @@ -111,6 +112,9 @@ class CacheModule(BaseCacheModule): self._prefix = C.CACHE_PLUGIN_PREFIX self._keys_set = 'ansible_cache_keys' + if not HAS_REDIS: + raise AnsibleError("The 'redis' python module (version 2.4.5 or newer) is required for the redis fact cache, 'pip install redis'") + self._cache = {} kw = {} diff --git a/plugins/inventory/linode.py b/plugins/inventory/linode.py index ce6c25c3a6..c308fb8213 100644 --- a/plugins/inventory/linode.py +++ b/plugins/inventory/linode.py @@ -63,8 +63,9 @@ from ansible.plugins.inventory import BaseInventoryPlugin try: from linode_api4 import LinodeClient from linode_api4.errors import ApiError as LinodeApiError + HAS_LINODE = True except ImportError: - raise AnsibleError('the Linode dynamic inventory plugin requires linode_api4.') + HAS_LINODE = False class InventoryModule(BaseInventoryPlugin): @@ -194,6 +195,9 @@ class InventoryModule(BaseInventoryPlugin): """Dynamically parse Linode the cloud inventory.""" super(InventoryModule, self).parse(inventory, loader, path) + if not HAS_LINODE: + raise AnsibleError('the Linode dynamic inventory plugin requires linode_api4.') + config_data = self._read_config_data(path) self._build_client()