From 09bfe42a5cad00227d624fcd59c4814219f88312 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Mon, 14 Jan 2019 19:44:59 -0500 Subject: [PATCH] Bubble up import exception content for k8s module (#50657) * Bubble up import exception content for k8s module Signed-off-by: Fabian von Feilitzsch * Track down other places import exception is reported * Add changelog fragment --- changelogs/fragments/50657-bubble-k8s-import-exception.yml | 3 +++ lib/ansible/module_utils/k8s/common.py | 6 ++++-- lib/ansible/plugins/inventory/k8s.py | 4 ++-- lib/ansible/plugins/lookup/k8s.py | 6 ++++-- 4 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/50657-bubble-k8s-import-exception.yml diff --git a/changelogs/fragments/50657-bubble-k8s-import-exception.yml b/changelogs/fragments/50657-bubble-k8s-import-exception.yml new file mode 100644 index 0000000000..cd8aca0b43 --- /dev/null +++ b/changelogs/fragments/50657-bubble-k8s-import-exception.yml @@ -0,0 +1,3 @@ +--- +bugfixes: +- k8s modules and plugins now bubble up error message when the openshift python client fails to import. diff --git a/lib/ansible/module_utils/k8s/common.py b/lib/ansible/module_utils/k8s/common.py index b285469f56..1488fc211b 100644 --- a/lib/ansible/module_utils/k8s/common.py +++ b/lib/ansible/module_utils/k8s/common.py @@ -32,8 +32,10 @@ try: from openshift.dynamic import DynamicClient from openshift.dynamic.exceptions import ResourceNotFoundError, ResourceNotUniqueError HAS_K8S_MODULE_HELPER = True -except ImportError: + k8s_import_exception = None +except ImportError as e: HAS_K8S_MODULE_HELPER = False + k8s_import_exception = e try: import yaml @@ -242,7 +244,7 @@ class KubernetesAnsibleModule(AnsibleModule, K8sAnsibleMixin): AnsibleModule.__init__(self, *args, **kwargs) if not HAS_K8S_MODULE_HELPER: - self.fail_json(msg="This module requires the OpenShift Python client. Try `pip install openshift`") + self.fail_json(msg="This module requires the OpenShift Python client. Try `pip install openshift`", error=str(k8s_import_exception)) self.openshift_version = openshift.__version__ if not HAS_YAML: diff --git a/lib/ansible/plugins/inventory/k8s.py b/lib/ansible/plugins/inventory/k8s.py index d2a7b60bad..642770175e 100644 --- a/lib/ansible/plugins/inventory/k8s.py +++ b/lib/ansible/plugins/inventory/k8s.py @@ -111,7 +111,7 @@ connections: import json from ansible.errors import AnsibleError -from ansible.module_utils.k8s.common import K8sAnsibleMixin, HAS_K8S_MODULE_HELPER +from ansible.module_utils.k8s.common import K8sAnsibleMixin, HAS_K8S_MODULE_HELPER, k8s_import_exception from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable try: @@ -151,7 +151,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable, K8sAnsibleM if not HAS_K8S_MODULE_HELPER: raise K8sInventoryException( - "This module requires the OpenShift Python client. Try `pip install openshift`" + "This module requires the OpenShift Python client. Try `pip install openshift`. Detail: {0}".format(k8s_import_exception) ) source_data = None diff --git a/lib/ansible/plugins/lookup/k8s.py b/lib/ansible/plugins/lookup/k8s.py index 1d742182ad..13965a2eb7 100644 --- a/lib/ansible/plugins/lookup/k8s.py +++ b/lib/ansible/plugins/lookup/k8s.py @@ -203,8 +203,10 @@ try: from openshift.dynamic import DynamicClient from openshift.dynamic.exceptions import NotFoundError HAS_K8S_MODULE_HELPER = True -except ImportError as exc: + k8s_import_exception = None +except ImportError as e: HAS_K8S_MODULE_HELPER = False + k8s_import_exception = e try: import yaml @@ -219,7 +221,7 @@ class KubernetesLookup(K8sAnsibleMixin): if not HAS_K8S_MODULE_HELPER: raise Exception( - "Requires the OpenShift Python client. Try `pip install openshift`" + "Requires the OpenShift Python client. Try `pip install openshift`. Detail: {0}".format(k8s_import_exception) ) if not HAS_YAML: