From 853236d6509415b5aa084ca868790104261bfdce Mon Sep 17 00:00:00 2001 From: Jim Gu Date: Fri, 26 Oct 2018 03:10:25 -0400 Subject: [PATCH] VMware: Avoid misleading PyVmomi error if requests import fails * Avoid misleading PyVmomi error if requests import fails Requests is imported by the VMware module_utils as an external dependency; however, because it is in a try/catch block containing the imports for PyVmomi, if requests fails to import properly, Ansible will instead complain about PyVmomi not being installed. By moving the import outside of the try/catch block, if requests fails to import, an error like the following will be returned: ImportError: No module named requests This should result in less confusion. * catch requests ImportError Signed-off-by: Jim Gu Signed-off-by: Abhijeet Kasurde (cherry picked from commit 99ee30768acce4f99137b58009ee14c24fef9937) --- .../47313-vmware_utils-handle_requests_import_error.yaml | 2 ++ lib/ansible/module_utils/vmware.py | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 changelogs/fragments/47313-vmware_utils-handle_requests_import_error.yaml diff --git a/changelogs/fragments/47313-vmware_utils-handle_requests_import_error.yaml b/changelogs/fragments/47313-vmware_utils-handle_requests_import_error.yaml new file mode 100644 index 00000000000..86130243685 --- /dev/null +++ b/changelogs/fragments/47313-vmware_utils-handle_requests_import_error.yaml @@ -0,0 +1,2 @@ +bugfixes: +- Avoid misleading PyVmomi error if requests import fails in vmware module utils. diff --git a/lib/ansible/module_utils/vmware.py b/lib/ansible/module_utils/vmware.py index 52b6d1ca474..093d44ca141 100644 --- a/lib/ansible/module_utils/vmware.py +++ b/lib/ansible/module_utils/vmware.py @@ -15,6 +15,11 @@ from random import randint try: # requests is required for exception handling of the ConnectionError import requests + HAS_REQUESTS = True +except ImportError: + HAS_REQUESTS = False + +try: from pyVim import connect from pyVmomi import vim, vmodl HAS_PYVMOMI = True @@ -764,6 +769,10 @@ class PyVmomi(object): """ Constructor """ + if not HAS_REQUESTS: + self.module.fail_json(msg="Unable to find 'requests' Python library which is required." + " Please install using 'pip install requests'") + if not HAS_PYVMOMI: module.fail_json(msg='PyVmomi Python module required. Install using "pip install PyVmomi"')