psexec: better error on import failure (#47327)

(cherry picked from commit 013c44484a)
This commit is contained in:
Jordan Borean 2018-10-19 16:56:16 +10:00 committed by Toshio Kuratomi
parent 69568b4ca8
commit a636562818
2 changed files with 16 additions and 7 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- psexec - give proper error message when the psexec requirements are not installed

View file

@ -305,6 +305,9 @@ rc:
sample: 0 sample: 0
''' '''
import sys
import traceback
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_bytes, to_text from ansible.module_utils._text import to_bytes, to_text
@ -317,8 +320,8 @@ try:
from smbprotocol.exceptions import SMBException, SMBAuthenticationError, \ from smbprotocol.exceptions import SMBException, SMBAuthenticationError, \
SMBResponseException SMBResponseException
HAS_PYPSEXEC = True HAS_PYPSEXEC = True
except ImportError as exc: except ImportError:
PYPSEXEC_IMP_ERR = exc PYPSEXEC_IMP_ERR = traceback.format_exc()
HAS_PYPSEXEC = False HAS_PYPSEXEC = False
KERBEROS_IMP_ERR = None KERBEROS_IMP_ERR = None
@ -327,8 +330,8 @@ try:
# GSSAPI extension required for Kerberos Auth in SMB # GSSAPI extension required for Kerberos Auth in SMB
from gssapi.raw import inquire_sec_context_by_oid from gssapi.raw import inquire_sec_context_by_oid
HAS_KERBEROS = True HAS_KERBEROS = True
except ImportError as exc: except ImportError:
KERBEROS_IMP_ERR = exc KERBEROS_IMP_ERR = traceback.format_exc()
HAS_KERBEROS = False HAS_KERBEROS = False
@ -387,7 +390,9 @@ def main():
'running as System: process_username, ' 'running as System: process_username, '
'process_password') 'process_password')
if not HAS_PYPSEXEC: if not HAS_PYPSEXEC:
module.fail_json(msg='The pypsexec python module is required', module.fail_json(msg="The pypsexec Python module is required to be "
"installed for the Python environment at '%s'"
% sys.executable,
exception=PYPSEXEC_IMP_ERR) exception=PYPSEXEC_IMP_ERR)
hostname = module.params['hostname'] hostname = module.params['hostname']
@ -421,8 +426,10 @@ def main():
if connection_username is None or connection_password is None and \ if connection_username is None or connection_password is None and \
not HAS_KERBEROS: not HAS_KERBEROS:
module.fail_json(msg='The gssapi python module with the GGF extension ' module.fail_json(msg="The gssapi Python module with the GGF extension "
'is required for Kerberos authentication', "used for kerberos auth is required to be "
"installed for the Python environment at '%s'"
% sys.executable,
exception=KERBEROS_IMP_ERR) exception=KERBEROS_IMP_ERR)
win_client = client.Client(server=hostname, username=connection_username, win_client = client.Client(server=hostname, username=connection_username,