diff --git a/changelogs/fragments/fix-plugin-imports.yml b/changelogs/fragments/fix-plugin-imports.yml index 93afa59bb0..aa3cbecfd5 100644 --- a/changelogs/fragments/fix-plugin-imports.yml +++ b/changelogs/fragments/fix-plugin-imports.yml @@ -2,3 +2,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)." +- "gcp_storage_files lookup 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/lookup/gcp_storage_file.py b/plugins/lookup/gcp_storage_file.py index 09181ffc9d..206788c311 100644 --- a/plugins/lookup/gcp_storage_file.py +++ b/plugins/lookup/gcp_storage_file.py @@ -48,11 +48,16 @@ import base64 import json import mimetypes import os -import requests from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase from ansible.utils.display import Display +try: + import requests + HAS_REQUESTS = True +except ImportError: + HAS_REQUESTS = False + try: from ansible_collections.google.cloud.plugins.module_utils.gcp_utils import navigate_hash, GcpSession HAS_GOOGLE_CLOUD_COLLECTION = True @@ -146,4 +151,6 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): if not HAS_GOOGLE_CLOUD_COLLECTION: raise AnsibleError("community.general.gcp_storage_file needs a supported version of the google.cloud collection installed") + if not HAS_REQUESTS: + raise AnsibleError("community.general.gcp_storage_file needs requests installed. Use `pip install requests` to install it") return GcpFileLookup().run(terms, variables=variables, **kwargs)