Merge branch 'winrm_v2_fixes' of https://github.com/cchurch/ansible into cchurch-winrm_v2_fixes

pull/11822/head
James Cammarata 9 years ago
commit 2a93559fc8

@ -37,6 +37,7 @@ REPLACER = "#<<INCLUDE_ANSIBLE_MODULE_COMMON>>"
REPLACER_ARGS = "\"<<INCLUDE_ANSIBLE_MODULE_ARGS>>\"" REPLACER_ARGS = "\"<<INCLUDE_ANSIBLE_MODULE_ARGS>>\""
REPLACER_COMPLEX = "\"<<INCLUDE_ANSIBLE_MODULE_COMPLEX_ARGS>>\"" REPLACER_COMPLEX = "\"<<INCLUDE_ANSIBLE_MODULE_COMPLEX_ARGS>>\""
REPLACER_WINDOWS = "# POWERSHELL_COMMON" REPLACER_WINDOWS = "# POWERSHELL_COMMON"
REPLACER_WINARGS = "<<INCLUDE_ANSIBLE_MODULE_WINDOWS_ARGS>>"
REPLACER_VERSION = "\"<<ANSIBLE_VERSION>>\"" REPLACER_VERSION = "\"<<ANSIBLE_VERSION>>\""
# We could end up writing out parameters with unicode characters so we need to # We could end up writing out parameters with unicode characters so we need to
@ -65,6 +66,8 @@ def _find_snippet_imports(module_data, module_path, strip_comments):
module_style = 'old' module_style = 'old'
if REPLACER in module_data: if REPLACER in module_data:
module_style = 'new' module_style = 'new'
elif REPLACER_WINDOWS in module_data:
module_style = 'new'
elif 'from ansible.module_utils.' in module_data: elif 'from ansible.module_utils.' in module_data:
module_style = 'new' module_style = 'new'
elif 'WANT_JSON' in module_data: elif 'WANT_JSON' in module_data:
@ -165,6 +168,7 @@ def modify_module(module_path, module_args, task_vars=dict(), strip_comments=Fal
# these strings should be part of the 'basic' snippet which is required to be included # these strings should be part of the 'basic' snippet which is required to be included
module_data = module_data.replace(REPLACER_VERSION, repr(__version__)) module_data = module_data.replace(REPLACER_VERSION, repr(__version__))
module_data = module_data.replace(REPLACER_COMPLEX, encoded_args) module_data = module_data.replace(REPLACER_COMPLEX, encoded_args)
module_data = module_data.replace(REPLACER_WINARGS, module_args_json.encode('utf-8'))
if module_style == 'new': if module_style == 'new':
facility = C.DEFAULT_SYSLOG_FACILITY facility = C.DEFAULT_SYSLOG_FACILITY

@ -127,6 +127,13 @@ class TaskExecutor:
return result return result
except AnsibleError, e: except AnsibleError, e:
return dict(failed=True, msg=to_unicode(e, nonstring='simplerepr')) return dict(failed=True, msg=to_unicode(e, nonstring='simplerepr'))
finally:
try:
self._connection.close()
except AttributeError:
pass
except Exception, e:
debug("error closing connection: %s" % to_unicode(e))
def _get_loop_items(self): def _get_loop_items(self):
''' '''

@ -26,8 +26,17 @@
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #
# Helper function to parse Ansible JSON arguments from a file passed as # Ansible v2 will insert the module arguments below as a string containing
# the single argument to the module # JSON; assign them to an environment variable and redefine $args so existing
# modules will continue to work.
$complex_args = @'
<<INCLUDE_ANSIBLE_MODULE_WINDOWS_ARGS>>
'@
Set-Content env:MODULE_COMPLEX_ARGS -Value $complex_args
$args = @('env:MODULE_COMPLEX_ARGS')
# Helper function to parse Ansible JSON arguments from a "file" passed as
# the single argument to the module.
# Example: $params = Parse-Args $args # Example: $params = Parse-Args $args
Function Parse-Args($arguments) Function Parse-Args($arguments)
{ {

@ -69,9 +69,29 @@ class ActionBase:
# Search module path(s) for named module. # Search module path(s) for named module.
module_suffixes = getattr(self._connection, 'default_suffixes', None) module_suffixes = getattr(self._connection, 'default_suffixes', None)
# Check to determine if PowerShell modules are supported, and apply
# some fixes (hacks) to module name + args.
if module_suffixes and '.ps1' in module_suffixes:
# Use Windows versions of stat/file/copy modules when called from
# within other action plugins.
if module_name in ('stat', 'file', 'copy') and self._task.action != module_name:
module_name = 'win_%s' % module_name
# Remove extra quotes surrounding path parameters before sending to module.
if module_name in ('win_stat', 'win_file', 'win_copy', 'slurp') and module_args and hasattr(self._connection._shell, '_unquote'):
for key in ('src', 'dest', 'path'):
if key in module_args:
module_args[key] = self._connection._shell._unquote(module_args[key])
module_path = self._shared_loader_obj.module_loader.find_plugin(module_name, module_suffixes) module_path = self._shared_loader_obj.module_loader.find_plugin(module_name, module_suffixes)
if module_path is None: if module_path is None:
module_path2 = self._shared_loader_obj.module_loader.find_plugin('ping', module_suffixes) # Use Windows version of ping module to check module paths when
# using a connection that supports .ps1 suffixes.
if module_suffixes and '.ps1' in module_suffixes:
ping_module = 'win_ping'
else:
ping_module = 'ping'
module_path2 = self._shared_loader_obj.module_loader.find_plugin(ping_module, module_suffixes)
if module_path2 is not None: if module_path2 is not None:
raise AnsibleError("The module %s was not found in configured module paths" % (module_name)) raise AnsibleError("The module %s was not found in configured module paths" % (module_name))
else: else:
@ -265,9 +285,10 @@ class ActionBase:
def _remote_expand_user(self, path, tmp): def _remote_expand_user(self, path, tmp):
''' takes a remote path and performs tilde expansion on the remote host ''' ''' takes a remote path and performs tilde expansion on the remote host '''
if not path.startswith('~'): if not path.startswith('~'): # FIXME: Windows paths may start with "~ instead of just ~
return path return path
# FIXME: Can't use os.path.sep for Windows paths.
split_path = path.split(os.path.sep, 1) split_path = path.split(os.path.sep, 1)
expand_path = split_path[0] expand_path = split_path[0]
if expand_path == '~': if expand_path == '~':
@ -340,6 +361,8 @@ class ActionBase:
remote_module_path = None remote_module_path = None
if not tmp and self._late_needs_tmp_path(tmp, module_style): if not tmp and self._late_needs_tmp_path(tmp, module_style):
tmp = self._make_tmp_path() tmp = self._make_tmp_path()
if tmp:
remote_module_path = self._connection._shell.join_path(tmp, module_name) remote_module_path = self._connection._shell.join_path(tmp, module_name)
# FIXME: async stuff here? # FIXME: async stuff here?
@ -457,7 +480,7 @@ class ActionBase:
if rc is None: if rc is None:
rc = 0 rc = 0
return dict(rc=rc, stdout=out, stderr=err) return dict(rc=rc, stdout=out, stdout_lines=out.splitlines(), stderr=err)
def _get_first_available_file(self, faf, of=None, searchdir='files'): def _get_first_available_file(self, faf, of=None, searchdir='files'):

@ -53,7 +53,7 @@ class ActionModule(ActionBase):
# Check if the source ends with a "/" # Check if the source ends with a "/"
source_trailing_slash = False source_trailing_slash = False
if source: if source:
source_trailing_slash = source.endswith(os.sep) source_trailing_slash = self._connection._shell.path_has_trailing_slash(source)
# Define content_tempfile in case we set it after finding content populated. # Define content_tempfile in case we set it after finding content populated.
content_tempfile = None content_tempfile = None
@ -182,7 +182,7 @@ class ActionModule(ActionBase):
continue continue
# Define a remote directory that we will copy the file to. # Define a remote directory that we will copy the file to.
tmp_src = tmp + 'source' tmp_src = self._connection._shell.join_path(tmp, 'source')
if not raw: if not raw:
self._connection.put_file(source_full, tmp_src) self._connection.put_file(source_full, tmp_src)

@ -78,6 +78,7 @@ class ActionModule(ActionBase):
# calculate the destination name # calculate the destination name
if os.path.sep not in self._connection._shell.join_path('a', ''): if os.path.sep not in self._connection._shell.join_path('a', ''):
source = self._connection._shell._unquote(source)
source_local = source.replace('\\', '/') source_local = source.replace('\\', '/')
else: else:
source_local = source source_local = source

@ -0,0 +1,28 @@
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.plugins.action import ActionBase
from ansible.plugins.action.copy import ActionModule as CopyActionModule
# Even though CopyActionModule inherits from ActionBase, we still need to
# directly inherit from ActionBase to appease the plugin loader.
class ActionModule(CopyActionModule, ActionBase):
pass

@ -0,0 +1,28 @@
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.plugins.action import ActionBase
from ansible.plugins.action.template import ActionModule as TemplateActionModule
# Even though TemplateActionModule inherits from ActionBase, we still need to
# directly inherit from ActionBase to appease the plugin loader.
class ActionModule(TemplateActionModule, ActionBase):
pass

@ -45,7 +45,7 @@ from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleFileNo
from ansible.plugins.connections import ConnectionBase from ansible.plugins.connections import ConnectionBase
from ansible.plugins import shell_loader from ansible.plugins import shell_loader
from ansible.utils.path import makedirs_safe from ansible.utils.path import makedirs_safe
from ansible.utils.unicode import to_bytes from ansible.utils.unicode import to_bytes, to_unicode
class Connection(ConnectionBase): class Connection(ConnectionBase):
'''WinRM connections over HTTP/HTTPS.''' '''WinRM connections over HTTP/HTTPS.'''
@ -94,7 +94,7 @@ class Connection(ConnectionBase):
endpoint = parse.urlunsplit((scheme, netloc, '/wsman', '', '')) endpoint = parse.urlunsplit((scheme, netloc, '/wsman', '', ''))
self._display.debug('WINRM CONNECT: transport=%s endpoint=%s' % (transport, endpoint), host=self._play_context.remote_addr) self._display.vvvvv('WINRM CONNECT: transport=%s endpoint=%s' % (transport, endpoint), host=self._play_context.remote_addr)
protocol = Protocol( protocol = Protocol(
endpoint, endpoint,
transport=transport, transport=transport,
@ -117,30 +117,30 @@ class Connection(ConnectionBase):
raise AnsibleError("the username/password specified for this server was incorrect") raise AnsibleError("the username/password specified for this server was incorrect")
elif code == 411: elif code == 411:
return protocol return protocol
self._display.debug('WINRM CONNECTION ERROR: %s' % err_msg, host=self._play_context.remote_addr) self._display.vvvvv('WINRM CONNECTION ERROR: %s' % err_msg, host=self._play_context.remote_addr)
continue continue
if exc: if exc:
raise AnsibleError(str(exc)) raise AnsibleError(str(exc))
def _winrm_exec(self, command, args=(), from_exec=False): def _winrm_exec(self, command, args=(), from_exec=False):
if from_exec: if from_exec:
self._display.debug("WINRM EXEC %r %r" % (command, args), host=self._play_context.remote_addr) self._display.vvvvv("WINRM EXEC %r %r" % (command, args), host=self._play_context.remote_addr)
else: else:
self._display.debugv("WINRM EXEC %r %r" % (command, args), host=self._play_context.remote_addr) self._display.vvvvvv("WINRM EXEC %r %r" % (command, args), host=self._play_context.remote_addr)
if not self.protocol: if not self.protocol:
self.protocol = self._winrm_connect() self.protocol = self._winrm_connect()
if not self.shell_id: if not self.shell_id:
self.shell_id = self.protocol.open_shell() self.shell_id = self.protocol.open_shell(codepage=65001) # UTF-8
command_id = None command_id = None
try: try:
command_id = self.protocol.run_command(self.shell_id, command, args) command_id = self.protocol.run_command(self.shell_id, to_bytes(command), map(to_bytes, args))
response = Response(self.protocol.get_command_output(self.shell_id, command_id)) response = Response(self.protocol.get_command_output(self.shell_id, command_id))
if from_exec: if from_exec:
self._display.debug('WINRM RESULT %r' % response, host=self._play_context.remote_addr) self._display.vvvvv('WINRM RESULT %r' % to_unicode(response), host=self._play_context.remote_addr)
else: else:
self._display.debugv('WINRM RESULT %r' % response, host=self._play_context.remote_addr) self._display.vvvvv('WINRM RESULT %r' % to_unicode(response), host=self._play_context.remote_addr)
self._display.debugv('WINRM STDOUT %s' % response.std_out, host=self._play_context.remote_addr) self._display.vvvvvv('WINRM STDOUT %s' % to_unicode(response.std_out), host=self._play_context.remote_addr)
self._display.debugv('WINRM STDERR %s' % response.std_err, host=self._play_context.remote_addr) self._display.vvvvvv('WINRM STDERR %s' % to_unicode(response.std_err), host=self._play_context.remote_addr)
return response return response
finally: finally:
if command_id: if command_id:
@ -153,34 +153,42 @@ class Connection(ConnectionBase):
def exec_command(self, cmd, tmp_path, in_data=None, sudoable=True): def exec_command(self, cmd, tmp_path, in_data=None, sudoable=True):
super(Connection, self).exec_command(cmd, tmp_path, in_data=in_data, sudoable=sudoable) super(Connection, self).exec_command(cmd, tmp_path, in_data=in_data, sudoable=sudoable)
cmd_parts = shlex.split(to_bytes(cmd), posix=False)
cmd = to_bytes(cmd) cmd_parts = map(to_unicode, cmd_parts)
cmd_parts = shlex.split(cmd, posix=False) script = None
cmd_ext = cmd_parts and self._shell._unquote(cmd_parts[0]).lower()[-4:] or ''
# Support running .ps1 files (via script/raw).
if cmd_ext == '.ps1':
script = ' '.join(['&'] + cmd_parts)
# Support running .bat/.cmd files; change back to the default system encoding instead of UTF-8.
elif cmd_ext in ('.bat', '.cmd'):
script = ' '.join(['[System.Console]::OutputEncoding = [System.Text.Encoding]::Default;', '&'] + cmd_parts)
# Encode the command if not already encoded; supports running simple PowerShell commands via raw.
elif '-EncodedCommand' not in cmd_parts:
script = ' '.join(cmd_parts)
if script:
cmd_parts = self._shell._encode_script(script, as_list=True)
if '-EncodedCommand' in cmd_parts: if '-EncodedCommand' in cmd_parts:
encoded_cmd = cmd_parts[cmd_parts.index('-EncodedCommand') + 1] encoded_cmd = cmd_parts[cmd_parts.index('-EncodedCommand') + 1]
decoded_cmd = base64.b64decode(encoded_cmd) decoded_cmd = to_unicode(base64.b64decode(encoded_cmd))
self._display.vvv("EXEC %s" % decoded_cmd, host=self._play_context.remote_addr) self._display.vvv("EXEC %s" % decoded_cmd, host=self._play_context.remote_addr)
else: else:
self._display.vvv("EXEC %s" % cmd, host=self._play_context.remote_addr) self._display.vvv("EXEC %s" % cmd, host=self._play_context.remote_addr)
# For script/raw support.
if cmd_parts and cmd_parts[0].lower().endswith('.ps1'):
script = self._shell._build_file_cmd(cmd_parts, quote_args=False)
cmd_parts = self._shell._encode_script(script, as_list=True)
try: try:
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:], from_exec=True) result = self._winrm_exec(cmd_parts[0], cmd_parts[1:], from_exec=True)
except Exception as e: except Exception as e:
traceback.print_exc() traceback.print_exc()
raise AnsibleError("failed to exec cmd %s" % cmd) raise AnsibleError("failed to exec cmd %s" % cmd)
result.std_out = to_bytes(result.std_out) result.std_out = to_unicode(result.std_out)
result.std_err = to_bytes(result.std_err) result.std_err = to_unicode(result.std_err)
return (result.status_code, '', result.std_out, result.std_err) return (result.status_code, '', result.std_out, result.std_err)
def put_file(self, in_path, out_path): def put_file(self, in_path, out_path):
super(Connection, self).put_file(in_path, out_path) super(Connection, self).put_file(in_path, out_path)
out_path = self._shell._unquote(out_path)
self._display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) self._display.vvv('PUT "%s" TO "%s"' % (in_path, out_path), host=self._play_context.remote_addr)
if not os.path.exists(in_path): if not os.path.exists(in_path):
raise AnsibleFileNotFound("file or module does not exist: %s" % in_path) raise AnsibleFileNotFound('file or module does not exist: "%s"' % in_path)
with open(in_path) as in_file: with open(in_path) as in_file:
in_size = os.path.getsize(in_path) in_size = os.path.getsize(in_path)
script_template = ''' script_template = '''
@ -206,20 +214,20 @@ class Connection(ConnectionBase):
out_path = out_path + '.ps1' out_path = out_path + '.ps1'
b64_data = base64.b64encode(out_data) b64_data = base64.b64encode(out_data)
script = script_template % (self._shell._escape(out_path), offset, b64_data, in_size) script = script_template % (self._shell._escape(out_path), offset, b64_data, in_size)
self._display.debug("WINRM PUT %s to %s (offset=%d size=%d)" % (in_path, out_path, offset, len(out_data)), host=self._play_context.remote_addr) self._display.vvvvv('WINRM PUT "%s" to "%s" (offset=%d size=%d)' % (in_path, out_path, offset, len(out_data)), host=self._play_context.remote_addr)
cmd_parts = self._shell._encode_script(script, as_list=True) cmd_parts = self._shell._encode_script(script, as_list=True)
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:]) result = self._winrm_exec(cmd_parts[0], cmd_parts[1:])
if result.status_code != 0: if result.status_code != 0:
raise IOError(result.std_err.encode('utf-8')) raise IOError(to_unicode(result.std_err))
except Exception: except Exception:
traceback.print_exc() traceback.print_exc()
raise AnsibleError("failed to transfer file to %s" % out_path) raise AnsibleError('failed to transfer file to "%s"' % out_path)
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)
out_path = out_path.replace('\\', '/') out_path = out_path.replace('\\', '/')
self._display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) self._display.vvv('FETCH "%s" TO "%s"' % (in_path, out_path), host=self._play_context.remote_addr)
buffer_size = 2**19 # 0.5MB chunks buffer_size = 2**19 # 0.5MB chunks
makedirs_safe(os.path.dirname(out_path)) makedirs_safe(os.path.dirname(out_path))
out_file = None out_file = None
@ -248,11 +256,11 @@ class Connection(ConnectionBase):
Exit 1; Exit 1;
} }
''' % dict(buffer_size=buffer_size, path=self._shell._escape(in_path), offset=offset) ''' % dict(buffer_size=buffer_size, path=self._shell._escape(in_path), offset=offset)
self._display.debug("WINRM FETCH %s to %s (offset=%d)" % (in_path, out_path, offset), host=self._play_context.remote_addr) self._display.vvvvv('WINRM FETCH "%s" to "%s" (offset=%d)' % (in_path, out_path, offset), host=self._play_context.remote_addr)
cmd_parts = self._shell._encode_script(script, as_list=True) cmd_parts = self._shell._encode_script(script, as_list=True)
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:]) result = self._winrm_exec(cmd_parts[0], cmd_parts[1:])
if result.status_code != 0: if result.status_code != 0:
raise IOError(result.std_err.encode('utf-8')) raise IOError(to_unicode(result.std_err))
if result.std_out.strip() == '[DIR]': if result.std_out.strip() == '[DIR]':
data = None data = None
else: else:
@ -272,7 +280,7 @@ class Connection(ConnectionBase):
offset += len(data) offset += len(data)
except Exception: except Exception:
traceback.print_exc() traceback.print_exc()
raise AnsibleError("failed to transfer file to %s" % out_path) raise AnsibleError('failed to transfer file to "%s"' % out_path)
finally: finally:
if out_file: if out_file:
out_file.close() out_file.close()

@ -24,7 +24,9 @@ import random
import shlex import shlex
import time import time
_common_args = ['PowerShell', '-NoProfile', '-NonInteractive'] from ansible.utils.unicode import to_bytes, to_unicode
_common_args = ['PowerShell', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Unrestricted']
# Primarily for testing, allow explicitly specifying PowerShell version via # Primarily for testing, allow explicitly specifying PowerShell version via
# an environment variable. # an environment variable.
@ -38,24 +40,32 @@ class ShellModule(object):
return '' return ''
def join_path(self, *args): def join_path(self, *args):
return os.path.join(*args).replace('/', '\\') parts = []
for arg in args:
arg = self._unquote(arg).replace('/', '\\')
parts.extend([a for a in arg.split('\\') if a])
path = '\\'.join(parts)
if path.startswith('~'):
return path
return '"%s"' % path
def path_has_trailing_slash(self, path): def path_has_trailing_slash(self, path):
# Allow Windows paths to be specified using either slash. # Allow Windows paths to be specified using either slash.
path = self._unquote(path)
return path.endswith('/') or path.endswith('\\') return path.endswith('/') or path.endswith('\\')
def chmod(self, mode, path): def chmod(self, mode, path):
return '' return ''
def remove(self, path, recurse=False): def remove(self, path, recurse=False):
path = self._escape(path) path = self._escape(self._unquote(path))
if recurse: if recurse:
return self._encode_script('''Remove-Item "%s" -Force -Recurse;''' % path) return self._encode_script('''Remove-Item "%s" -Force -Recurse;''' % path)
else: else:
return self._encode_script('''Remove-Item "%s" -Force;''' % path) return self._encode_script('''Remove-Item "%s" -Force;''' % path)
def mkdtemp(self, basefile, system=False, mode=None): def mkdtemp(self, basefile, system=False, mode=None):
basefile = self._escape(basefile) basefile = self._escape(self._unquote(basefile))
# FIXME: Support system temp path! # FIXME: Support system temp path!
return self._encode_script('''(New-Item -Type Directory -Path $env:temp -Name "%s").FullName | Write-Host -Separator '';''' % basefile) return self._encode_script('''(New-Item -Type Directory -Path $env:temp -Name "%s").FullName | Write-Host -Separator '';''' % basefile)
@ -63,16 +73,17 @@ class ShellModule(object):
# PowerShell only supports "~" (not "~username"). Resolve-Path ~ does # PowerShell only supports "~" (not "~username"). Resolve-Path ~ does
# not seem to work remotely, though by default we are always starting # not seem to work remotely, though by default we are always starting
# in the user's home directory. # in the user's home directory.
user_home_path = self._unquote(user_home_path)
if user_home_path == '~': if user_home_path == '~':
script = 'Write-Host (Get-Location).Path' script = 'Write-Host (Get-Location).Path'
elif user_home_path.startswith('~\\'): elif user_home_path.startswith('~\\'):
script = 'Write-Host ((Get-Location).Path + "%s")' % _escape(user_home_path[1:]) script = 'Write-Host ((Get-Location).Path + "%s")' % self._escape(user_home_path[1:])
else: else:
script = 'Write-Host "%s"' % _escape(user_home_path) script = 'Write-Host "%s"' % self._escape(user_home_path)
return self._encode_script(script) return self._encode_script(script)
def checksum(self, path, *args, **kwargs): def checksum(self, path, *args, **kwargs):
path = self._escape(path) path = self._escape(self._unquote(path))
script = ''' script = '''
If (Test-Path -PathType Leaf "%(path)s") If (Test-Path -PathType Leaf "%(path)s")
{ {
@ -93,16 +104,36 @@ class ShellModule(object):
return self._encode_script(script) return self._encode_script(script)
def build_module_command(self, env_string, shebang, cmd, rm_tmp=None): def build_module_command(self, env_string, shebang, cmd, rm_tmp=None):
cmd = cmd.encode('utf-8') cmd_parts = shlex.split(to_bytes(cmd), posix=False)
cmd_parts = shlex.split(cmd, posix=False) cmd_parts = map(to_unicode, cmd_parts)
if not cmd_parts[0].lower().endswith('.ps1'): if shebang and shebang.lower() == '#!powershell':
cmd_parts[0] = '%s.ps1' % cmd_parts[0] if not self._unquote(cmd_parts[0]).lower().endswith('.ps1'):
script = self._build_file_cmd(cmd_parts) cmd_parts[0] = '"%s.ps1"' % self._unquote(cmd_parts[0])
cmd_parts.insert(0, '&')
elif shebang and shebang.startswith('#!'):
cmd_parts.insert(0, shebang[2:])
catch = '''
$_obj = @{ failed = $true; $msg = $_ }
echo $_obj | ConvertTo-Json -Compress -Depth 99
Exit 1
'''
script = 'Try { %s }\nCatch { %s }' % (' '.join(cmd_parts), 'throw')
if rm_tmp: if rm_tmp:
rm_tmp = self._escape(rm_tmp) rm_tmp = self._escape(self._unquote(rm_tmp))
script = '%s; Remove-Item "%s" -Force -Recurse;' % (script, rm_tmp) rm_cmd = 'Remove-Item "%s" -Force -Recurse -ErrorAction SilentlyContinue' % rm_tmp
script = '%s\nFinally { %s }' % (script, rm_cmd)
return self._encode_script(script) return self._encode_script(script)
def _unquote(self, value):
'''Remove any matching quotes that wrap the given value.'''
m = re.match(r'^\s*?\'(.*?)\'\s*?$', value)
if m:
return m.group(1)
m = re.match(r'^\s*?"(.*?)"\s*?$', value)
if m:
return m.group(1)
return value
def _escape(self, value, include_vars=False): def _escape(self, value, include_vars=False):
'''Return value escaped for use in PowerShell command.''' '''Return value escaped for use in PowerShell command.'''
# http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences # http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
@ -119,14 +150,10 @@ class ShellModule(object):
def _encode_script(self, script, as_list=False): def _encode_script(self, script, as_list=False):
'''Convert a PowerShell script to a single base64-encoded command.''' '''Convert a PowerShell script to a single base64-encoded command.'''
script = to_unicode(script)
script = '\n'.join([x.strip() for x in script.splitlines() if x.strip()]) script = '\n'.join([x.strip() for x in script.splitlines() if x.strip()])
encoded_script = base64.b64encode(script.encode('utf-16-le')) encoded_script = base64.b64encode(script.encode('utf-16-le'))
cmd_parts = _common_args + ['-EncodedCommand', encoded_script] cmd_parts = _common_args + ['-EncodedCommand', encoded_script]
if as_list: if as_list:
return cmd_parts return cmd_parts
return ' '.join(cmd_parts) return ' '.join(cmd_parts)
def _build_file_cmd(self, cmd_parts):
'''Build command line to run a file, given list of file name plus args.'''
return ' '.join(_common_args + ['-ExecutionPolicy', 'Unrestricted', '-File'] + ['"%s"' % x for x in cmd_parts])

@ -1,5 +1,5 @@
--- ---
win_output_dir: 'C:/temp/' win_output_dir: 'C:\ansible_testing'
output_dir: ~/ansible_testing output_dir: ~/ansible_testing
non_root_test_user: ansible non_root_test_user: ansible
pip_test_package: epdb pip_test_package: epdb

@ -73,16 +73,14 @@
- "fetch_flat_stat.stat.isreg" - "fetch_flat_stat.stat.isreg"
- "fetch_flat_stat.stat.md5 == fetch_flat.md5sum" - "fetch_flat_stat.stat.md5 == fetch_flat.md5sum"
- name: fetch a small file to flat directory (without trailing slash) #- name: fetch a small file to flat directory (without trailing slash)
fetch: src="C:/Windows/win.ini" dest="{{ output_dir }}" flat=yes # fetch: src="C:/Windows/win.ini" dest="{{ output_dir }}" flat=yes
register: fetch_flat_dir # register: fetch_flat_dir
ignore_errors: true
- name: check fetch flat to directory result #- name: check fetch flat to directory result
assert: # assert:
that: # that:
- "fetch_flat_dir|failed" # - "not fetch_flat_dir|changed"
- "fetch_flat_dir.msg"
- name: fetch a large binary file - name: fetch a large binary file
fetch: src="C:/Windows/explorer.exe" dest={{ output_dir }} fetch: src="C:/Windows/explorer.exe" dest={{ output_dir }}
@ -114,7 +112,7 @@
- "not fetch_large_again.changed" - "not fetch_large_again.changed"
- name: fetch a small file using backslashes in src path - name: fetch a small file using backslashes in src path
fetch: src="C:\Windows\system.ini" dest={{ output_dir }} fetch: src="C:\\Windows\\system.ini" dest={{ output_dir }}
register: fetch_small_bs register: fetch_small_bs
- name: check fetch small result with backslashes - name: check fetch small result with backslashes
@ -157,7 +155,7 @@
- "not fetch_missing|changed" - "not fetch_missing|changed"
- name: attempt to fetch a directory - name: attempt to fetch a directory
fetch: src="C:\Windows" dest={{ output_dir }} fetch: src="C:\\Windows" dest={{ output_dir }}
register: fetch_dir register: fetch_dir
ignore_errors: true ignore_errors: true

@ -32,7 +32,7 @@
# - "file_result.state == 'file'" # - "file_result.state == 'file'"
- name: verify that we are checking an absent file - name: verify that we are checking an absent file
win_file: path={{win_output_dir}}\bar.txt state=absent win_file: path={{win_output_dir}}/bar.txt state=absent
register: file2_result register: file2_result
- name: verify that the file was marked as changed - name: verify that the file was marked as changed
@ -42,7 +42,7 @@
# - "file2_result.state == 'absent'" # - "file2_result.state == 'absent'"
- name: verify we can touch a file - name: verify we can touch a file
win_file: path={{win_output_dir}}\baz.txt state=touch win_file: path={{win_output_dir}}/baz.txt state=touch
register: file3_result register: file3_result
- name: verify that the file was marked as changed - name: verify that the file was marked as changed
@ -85,8 +85,8 @@
# - "chown_result.failed == True" # - "chown_result.failed == True"
# - "file_exists_result.stat.exists == False" # - "file_exists_result.stat.exists == False"
# #
- name: clean up #- name: clean up
win_file: path=/tmp/worldwritable state=absent # win_file: path=/tmp/worldwritable state=absent
#- name: create soft link to file #- name: create soft link to file
# win_file: src={{output_file}} dest={{win_output_dir}}/soft.txt state=link # win_file: src={{output_file}} dest={{win_output_dir}}/soft.txt state=link
@ -107,7 +107,7 @@
# - "file6_result.changed == true" # - "file6_result.changed == true"
# #
- name: create a directory - name: create a directory
win_file: path={{win_output_dir}}\foobar state=directory win_file: path={{win_output_dir}}/foobar state=directory
register: file7_result register: file7_result
- debug: var=file7_result - debug: var=file7_result
@ -134,22 +134,22 @@
# when: selinux_installed.stdout != "" and selinux_enabled.stdout != "Disabled" # when: selinux_installed.stdout != "" and selinux_enabled.stdout != "Disabled"
- name: remote directory foobar - name: remote directory foobar
win_file: path={{win_output_dir}}\foobar state=absent win_file: path={{win_output_dir}}/foobar state=absent
- name: remove file foo.txt - name: remove file foo.txt
win_file: path={{win_output_dir}}\foo.txt state=absent win_file: path={{win_output_dir}}/foo.txt state=absent
- name: remove file bar.txt - name: remove file bar.txt
win_file: path={{win_output_dir}}\foo.txt state=absent win_file: path={{win_output_dir}}/foo.txt state=absent
- name: remove file baz.txt - name: remove file baz.txt
win_file: path={{win_output_dir}}\foo.txt state=absent win_file: path={{win_output_dir}}/foo.txt state=absent
- name: win copy directory structure over - name: win copy directory structure over
win_copy: src=foobar dest={{win_output_dir}} win_copy: src=foobar dest={{win_output_dir}}
- name: remove directory foobar - name: remove directory foobar
win_file: path={{win_output_dir}}\foobar state=absent win_file: path={{win_output_dir}}/foobar state=absent
register: file14_result register: file14_result
- debug: var=file14_result - debug: var=file14_result

@ -17,7 +17,7 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- name: use win_get_url module to download msi - name: use win_get_url module to download msi
win_get_url: url=http://downloads.sourceforge.net/project/sevenzip/7-Zip/9.22/7z922-x64.msi dest='C:\7z922-x64.msi' win_get_url: url=http://downloads.sourceforge.net/project/sevenzip/7-Zip/9.22/7z922-x64.msi dest='C:\\7z922-x64.msi'
register: win_get_url_result register: win_get_url_result
- name: install 7zip msi - name: install 7zip msi

@ -28,7 +28,7 @@
- "win_ping_result.ping == 'pong'" - "win_ping_result.ping == 'pong'"
- name: test win_ping with data - name: test win_ping with data
win_ping: data=blah win_ping: data=
register: win_ping_with_data_result register: win_ping_with_data_result
- name: check win_ping result with data - name: check win_ping result with data
@ -36,21 +36,11 @@
that: that:
- "not win_ping_with_data_result|failed" - "not win_ping_with_data_result|failed"
- "not win_ping_with_data_result|changed" - "not win_ping_with_data_result|changed"
- "win_ping_with_data_result.ping == 'blah'" - "win_ping_with_data_result.ping == ''"
#- name: test local ping (should use default ping) - name: test win_ping.ps1 with data as complex args
# local_action: ping win_ping.ps1:
# register: local_ping_result data: bleep
#- name: check local ping result
# assert:
# that:
# - "not local_ping_result|failed"
# - "not local_ping_result|changed"
# - "local_ping_result.ping == 'pong'"
- name: test win_ping.ps1 with data
win_ping.ps1: data=bleep
register: win_ping_ps1_result register: win_ping_ps1_result
- name: check win_ping.ps1 result with data - name: check win_ping.ps1 result with data
@ -60,13 +50,32 @@
- "not win_ping_ps1_result|changed" - "not win_ping_ps1_result|changed"
- "win_ping_ps1_result.ping == 'bleep'" - "win_ping_ps1_result.ping == 'bleep'"
#- name: test win_ping with invalid args - name: test win_ping with extra args to verify that v2 module replacer escaping works as expected
# win_ping: arg=invalid win_ping:
# register: win_ping_ps1_invalid_args_result data: bloop
a_null: null
#- name: check that win_ping.ps1 with invalid args fails a_boolean: true
# assert: another_boolean: false
# that: a_number: 299792458
# - "win_ping_ps1_invalid_args_result|failed" another_number: 22.7
# - "win_ping_ps1_invalid_args_result.msg" yet_another_number: 6.022e23
a_string: |
it's magic
"@'
'@"
an_array:
- first
- 2
- 3.0
an_object:
- the_thing: the_value
- the_other_thing: 0
- the_list_of_things: [1, 2, 3, 5]
register: win_ping_extra_args_result
- name: check that win_ping with extra args succeeds and ignores everything except data
assert:
that:
- "not win_ping_extra_args_result|failed"
- "not win_ping_extra_args_result|changed"
- "win_ping_extra_args_result.ping == 'bloop'"

@ -72,7 +72,7 @@
- "not unknown_result|changed" - "not unknown_result|changed"
- name: run a command that takes longer than 60 seconds - name: run a command that takes longer than 60 seconds
raw: PowerShell -Command Start-Sleep -s 75 raw: Start-Sleep -s 75
register: sleep_command register: sleep_command
- name: assert that the sleep command ran - name: assert that the sleep command ran

@ -3,3 +3,4 @@
# Parameters to pass to test scripts. # Parameters to pass to test scripts.
test_win_script_value: VaLuE test_win_script_value: VaLuE
test_win_script_splat: "@{This='THIS'; That='THAT'; Other='OTHER'}" test_win_script_splat: "@{This='THIS'; That='THAT'; Other='OTHER'}"
test_win_script_filename: "C:/Users/{{ansible_ssh_user}}/testing_win_script.txt"

@ -0,0 +1,2 @@
@ECHO OFF
ECHO We can even run a batch file with cmd extension!

@ -0,0 +1,3 @@
# Test script to create a file.
echo $null > $args[0]

@ -0,0 +1,3 @@
# Test script to remove a file.
Remove-Item $args[0] -Force

@ -30,24 +30,24 @@
- "not test_script_result|failed" - "not test_script_result|failed"
- "test_script_result|changed" - "test_script_result|changed"
- name: run test script that takes arguments - name: run test script that takes arguments including a unicode char
script: test_script_with_args.ps1 /this /that /other script: test_script_with_args.ps1 /this /that /Ӧther
register: test_script_with_args_result register: test_script_with_args_result
- name: check that script ran and received arguments - name: check that script ran and received arguments and returned unicode
assert: assert:
that: that:
- "test_script_with_args_result.rc == 0" - "test_script_with_args_result.rc == 0"
- "test_script_with_args_result.stdout" - "test_script_with_args_result.stdout"
- "test_script_with_args_result.stdout_lines[0] == '/this'" - "test_script_with_args_result.stdout_lines[0] == '/this'"
- "test_script_with_args_result.stdout_lines[1] == '/that'" - "test_script_with_args_result.stdout_lines[1] == '/that'"
- "test_script_with_args_result.stdout_lines[2] == '/other'" - "test_script_with_args_result.stdout_lines[2] == '/Ӧther'"
- "not test_script_with_args_result.stderr" - "not test_script_with_args_result.stderr"
- "not test_script_with_args_result|failed" - "not test_script_with_args_result|failed"
- "test_script_with_args_result|changed" - "test_script_with_args_result|changed"
- name: run test script that takes parameters passed via splatting - name: run test script that takes parameters passed via splatting
script: test_script_with_splatting.ps1 "@{ This = 'this'; That = '{{ test_win_script_value }}'; Other = 'other'}" script: test_script_with_splatting.ps1 @{ This = 'this'; That = '{{ test_win_script_value }}'; Other = 'other'}
register: test_script_with_splatting_result register: test_script_with_splatting_result
- name: check that script ran and received parameters via splatting - name: check that script ran and received parameters via splatting
@ -63,7 +63,7 @@
- "test_script_with_splatting_result|changed" - "test_script_with_splatting_result|changed"
- name: run test script that takes splatted parameters from a variable - name: run test script that takes splatted parameters from a variable
script: test_script_with_splatting.ps1 {{ test_win_script_splat|quote }} script: test_script_with_splatting.ps1 {{ test_win_script_splat }}
register: test_script_with_splatting2_result register: test_script_with_splatting2_result
- name: check that script ran and received parameters via splatting from a variable - name: check that script ran and received parameters via splatting from a variable
@ -92,6 +92,58 @@
- "test_script_with_errors_result|failed" - "test_script_with_errors_result|failed"
- "test_script_with_errors_result|changed" - "test_script_with_errors_result|changed"
- name: cleanup test file if it exists
raw: Remove-Item "{{test_win_script_filename}}" -Force
ignore_errors: true
- name: run test script that creates a file
script: test_script_creates_file.ps1 "{{test_win_script_filename}}" creates="{{test_win_script_filename}}"
register: test_script_creates_file_result
- name: check that script ran and indicated a change
assert:
that:
- "test_script_creates_file_result.rc == 0"
- "not test_script_creates_file_result.stdout"
- "not test_script_creates_file_result.stderr"
- "not test_script_creates_file_result|failed"
- "test_script_creates_file_result|changed"
- name: run test script that creates a file again
script: test_script_creates_file.ps1 "{{test_win_script_filename}}" creates="{{test_win_script_filename}}"
register: test_script_creates_file_again_result
- name: check that the script did not run since the remote file exists
assert:
that:
- "not test_script_creates_file_again_result|failed"
- "not test_script_creates_file_again_result|changed"
- "test_script_creates_file_again_result|skipped"
- name: run test script that removes a file
script: test_script_removes_file.ps1 "{{test_win_script_filename}}" removes="{{test_win_script_filename}}"
register: test_script_removes_file_result
- name: check that the script ran since the remote file exists
assert:
that:
- "test_script_removes_file_result.rc == 0"
- "not test_script_removes_file_result.stdout"
- "not test_script_removes_file_result.stderr"
- "not test_script_removes_file_result|failed"
- "test_script_removes_file_result|changed"
- name: run test script that removes a file again
script: test_script_removes_file.ps1 "{{test_win_script_filename}}" removes="{{test_win_script_filename}}"
register: test_script_removes_file_again_result
- name: check that the script did not run since the remote file does not exist
assert:
that:
- "not test_script_removes_file_again_result|failed"
- "not test_script_removes_file_again_result|changed"
- "test_script_removes_file_again_result|skipped"
- name: run simple batch file - name: run simple batch file
script: test_script.bat script: test_script.bat
register: test_batch_result register: test_batch_result
@ -105,3 +157,17 @@
- "not test_batch_result.stderr" - "not test_batch_result.stderr"
- "not test_batch_result|failed" - "not test_batch_result|failed"
- "test_batch_result|changed" - "test_batch_result|changed"
- name: run simple batch file with .cmd extension
script: test_script.cmd
register: test_cmd_result
- name: check that batch file with .cmd extension ran
assert:
that:
- "test_cmd_result.rc == 0"
- "test_cmd_result.stdout"
- "'cmd extension' in test_cmd_result.stdout"
- "not test_cmd_result.stderr"
- "not test_cmd_result|failed"
- "test_cmd_result|changed"

@ -20,7 +20,7 @@
action: setup action: setup
register: setup_result register: setup_result
- name: check setup result - name: check windows setup result
assert: assert:
that: that:
- "not setup_result|failed" - "not setup_result|failed"
@ -38,6 +38,8 @@
- "setup_result.ansible_facts.ansible_interfaces[0]" - "setup_result.ansible_facts.ansible_interfaces[0]"
- "setup_result.ansible_facts.ansible_interfaces[0].interface_name" - "setup_result.ansible_facts.ansible_interfaces[0].interface_name"
- "setup_result.ansible_facts.ansible_interfaces[0].interface_index" - "setup_result.ansible_facts.ansible_interfaces[0].interface_index"
- "setup_result.ansible_facts.ansible_architecture"
- "setup_result.ansible_facts.ansible_os_name"
- "setup_result.ansible_facts.ansible_powershell_version" - "setup_result.ansible_facts.ansible_powershell_version"
- name: check setup result only when using https - name: check setup result only when using https

@ -27,6 +27,12 @@
- "not win_stat_file.stat.isdir" - "not win_stat_file.stat.isdir"
- "win_stat_file.stat.size > 0" - "win_stat_file.stat.size > 0"
- "win_stat_file.stat.md5" - "win_stat_file.stat.md5"
- "win_stat_file.stat.extension"
- "win_stat_file.stat.attributes"
- "win_stat_file.stat.owner"
- "win_stat_file.stat.creationtime"
- "win_stat_file.stat.lastaccesstime"
- "win_stat_file.stat.lastwritetime"
- "not win_stat_file|failed" - "not win_stat_file|failed"
- "not win_stat_file|changed" - "not win_stat_file|changed"
@ -34,13 +40,19 @@
win_stat: path="C:\Windows\win.ini" get_md5=no win_stat: path="C:\Windows\win.ini" get_md5=no
register: win_stat_file_no_md5 register: win_stat_file_no_md5
- name: check win_stat file result without md - name: check win_stat file result without md5
assert: assert:
that: that:
- "win_stat_file_no_md5.stat.exists" - "win_stat_file_no_md5.stat.exists"
- "not win_stat_file_no_md5.stat.isdir" - "not win_stat_file_no_md5.stat.isdir"
- "win_stat_file_no_md5.stat.size > 0" - "win_stat_file_no_md5.stat.size > 0"
- "not win_stat_file_no_md5.stat.md5|default('')" - "not win_stat_file_no_md5.stat.md5|default('')"
- "win_stat_file_no_md5.stat.extension"
- "win_stat_file_no_md5.stat.attributes"
- "win_stat_file_no_md5.stat.owner"
- "win_stat_file_no_md5.stat.creationtime"
- "win_stat_file_no_md5.stat.lastaccesstime"
- "win_stat_file_no_md5.stat.lastwritetime"
- "not win_stat_file_no_md5|failed" - "not win_stat_file_no_md5|failed"
- "not win_stat_file_no_md5|changed" - "not win_stat_file_no_md5|changed"
@ -53,6 +65,12 @@
that: that:
- "win_stat_dir.stat.exists" - "win_stat_dir.stat.exists"
- "win_stat_dir.stat.isdir" - "win_stat_dir.stat.isdir"
- "win_stat_dir.stat.extension == ''"
- "win_stat_dir.stat.attributes"
- "win_stat_dir.stat.owner"
- "win_stat_dir.stat.creationtime"
- "win_stat_dir.stat.lastaccesstime"
- "win_stat_dir.stat.lastwritetime"
- "not win_stat_dir|failed" - "not win_stat_dir|failed"
- "not win_stat_dir|changed" - "not win_stat_dir|changed"

@ -42,10 +42,10 @@
# VERIFY CONTENTS # VERIFY CONTENTS
- name: copy known good into place - name: copy known good into place
win_copy: src=foo.txt dest={{win_output_dir}}\foo.txt win_copy: src=foo.txt dest={{win_output_dir}}\\foo.txt
- name: compare templated file to known good - name: compare templated file to known good
raw: fc.exe {{win_output_dir}}\foo.templated {{win_output_dir}}\foo.txt raw: fc.exe {{win_output_dir}}\\foo.templated {{win_output_dir}}\\foo.txt
register: diff_result register: diff_result
- debug: var=diff_result - debug: var=diff_result

@ -51,7 +51,7 @@
- "win_user_missing_query_result.state == 'absent'" - "win_user_missing_query_result.state == 'absent'"
- name: test create user - name: test create user
win_user: name="{{ test_win_user_name }}" password="{{ test_win_user_password }}" groups="Guests" win_user: name="{{ test_win_user_name }}" password="{{ test_win_user_password }}" fullname="Test User" description="Test user account" groups="Guests"
register: win_user_create_result register: win_user_create_result
- name: check user creation result - name: check user creation result
@ -59,7 +59,8 @@
that: that:
- "win_user_create_result|changed" - "win_user_create_result|changed"
- "win_user_create_result.name == '{{ test_win_user_name }}'" - "win_user_create_result.name == '{{ test_win_user_name }}'"
- "win_user_create_result.fullname == '{{ test_win_user_name }}'" - "win_user_create_result.fullname == 'Test User'"
- "win_user_create_result.description == 'Test user account'"
- "win_user_create_result.path" - "win_user_create_result.path"
- "win_user_create_result.state == 'present'" - "win_user_create_result.state == 'present'"

Loading…
Cancel
Save