@ -25,11 +25,11 @@ import re
import shlex
import shlex
import traceback
import traceback
import json
import json
import xmltodict
from ansible . compat . six . moves . urllib . parse import urlunsplit
from ansible . compat . six . moves . urllib . parse import urlunsplit
from ansible . errors import AnsibleError , AnsibleConnectionFailure
from ansible . errors import AnsibleError , AnsibleConnectionFailure
try :
try :
import winrm
import winrm
from winrm import Response
from winrm import Response
@ -37,6 +37,11 @@ try:
except ImportError :
except ImportError :
raise AnsibleError ( " winrm is not installed " )
raise AnsibleError ( " winrm is not installed " )
try :
import xmltodict
except ImportError :
raise AnsibleError ( " xmltodict is not installed " )
HAVE_KERBEROS = False
HAVE_KERBEROS = False
try :
try :
import kerberos
import kerberos
@ -49,7 +54,6 @@ from ansible.plugins.connection import ConnectionBase
from ansible . utils . hashing import secure_hash
from ansible . utils . hashing import secure_hash
from ansible . utils . path import makedirs_safe
from ansible . utils . path import makedirs_safe
from ansible . utils . unicode import to_bytes , to_unicode , to_str
from ansible . utils . unicode import to_bytes , to_unicode , to_str
from ansible . utils . vars import combine_vars
try :
try :
from __main__ import display
from __main__ import display
@ -57,7 +61,6 @@ except ImportError:
from ansible . utils . display import Display
from ansible . utils . display import Display
display = Display ( )
display = Display ( )
class Connection ( ConnectionBase ) :
class Connection ( ConnectionBase ) :
''' WinRM connections over HTTP/HTTPS. '''
''' WinRM connections over HTTP/HTTPS. '''
@ -182,7 +185,7 @@ class Connection(ConnectionBase):
stream [ ' #text ' ] = base64 . b64encode ( to_bytes ( stdin ) )
stream [ ' #text ' ] = base64 . b64encode ( to_bytes ( stdin ) )
if eof :
if eof :
stream [ ' @End ' ] = ' true '
stream [ ' @End ' ] = ' true '
rs = protocol. send_message ( xmltodict . unparse ( rq ) )
protocol. send_message ( xmltodict . unparse ( rq ) )
def _winrm_exec ( self , command , args = ( ) , from_exec = False , stdin_iterator = None ) :
def _winrm_exec ( self , command , args = ( ) , from_exec = False , stdin_iterator = None ) :
if not self . protocol :
if not self . protocol :
@ -195,7 +198,7 @@ class Connection(ConnectionBase):
command_id = None
command_id = None
try :
try :
stdin_push_failed = False
stdin_push_failed = False
command_id = self . protocol . run_command ( self . shell_id , to_bytes ( command ) , map ( to_bytes , args ) , console_mode_stdin = ( stdin_iterator == None ) )
command_id = self . protocol . run_command ( self . shell_id , to_bytes ( command ) , map ( to_bytes , args ) , console_mode_stdin = ( stdin_iterator is None ) )
# TODO: try/except around this, so we can get/return the command result on a broken pipe or other failure (probably more useful than the 500 that comes from this)
# TODO: try/except around this, so we can get/return the command result on a broken pipe or other failure (probably more useful than the 500 that comes from this)
try :
try :
@ -220,10 +223,10 @@ class Connection(ConnectionBase):
display . vvvvv ( ' WINRM RESULT %r ' % to_unicode ( response ) , host = self . _winrm_host )
display . vvvvv ( ' WINRM RESULT %r ' % to_unicode ( response ) , host = self . _winrm_host )
else :
else :
display . vvvvvv ( ' WINRM RESULT %r ' % to_unicode ( response ) , host = self . _winrm_host )
display . vvvvvv ( ' WINRM RESULT %r ' % to_unicode ( response ) , host = self . _winrm_host )
display . vvvvvv ( ' WINRM STDOUT %s ' % to_unicode ( response . std_out ) , host = self . _winrm_host )
display . vvvvvv ( ' WINRM STDOUT %s ' % to_unicode ( response . std_out ) , host = self . _winrm_host )
display . vvvvvv ( ' WINRM STDERR %s ' % to_unicode ( response . std_err ) , host = self . _winrm_host )
display . vvvvvv ( ' WINRM STDERR %s ' % to_unicode ( response . std_err ) , host = self . _winrm_host )
if stdin_push_failed :
if stdin_push_failed :
raise AnsibleError ( ' winrm send_input failed; \n stdout: %s \n stderr %s ' % ( response . std_out , response . std_err ) )
raise AnsibleError ( ' winrm send_input failed; \n stdout: %s \n stderr %s ' % ( response . std_out , response . std_err ) )
@ -367,7 +370,6 @@ class Connection(ConnectionBase):
if not remote_sha1 == local_sha1 :
if not remote_sha1 == local_sha1 :
raise AnsibleError ( " Remote sha1 hash {0} does not match local hash {1} " . format ( to_str ( remote_sha1 ) , to_str ( local_sha1 ) ) )
raise AnsibleError ( " Remote sha1 hash {0} does not match local hash {1} " . format ( to_str ( remote_sha1 ) , to_str ( local_sha1 ) ) )
def fetch_file ( self , in_path , out_path ) :
def fetch_file ( self , in_path , out_path ) :
super ( Connection , self ) . fetch_file ( in_path , out_path )
super ( Connection , self ) . fetch_file ( in_path , out_path )
in_path = self . _shell . _unquote ( in_path )
in_path = self . _shell . _unquote ( in_path )