windows environment support (#17402)
This commit is contained in:
parent
b6d24be09e
commit
b860b2d258
1 changed files with 26 additions and 2 deletions
|
@ -22,6 +22,8 @@ import os
|
||||||
import re
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
|
from ansible.compat.six import text_type
|
||||||
|
from ansible.errors import AnsibleError
|
||||||
from ansible.utils.unicode import to_bytes, to_unicode
|
from ansible.utils.unicode import to_bytes, to_unicode
|
||||||
|
|
||||||
_common_args = ['PowerShell', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Unrestricted']
|
_common_args = ['PowerShell', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Unrestricted']
|
||||||
|
@ -41,8 +43,29 @@ class ShellModule(object):
|
||||||
# Family of shells this has. Must match the filename without extension
|
# Family of shells this has. Must match the filename without extension
|
||||||
SHELL_FAMILY = 'powershell'
|
SHELL_FAMILY = 'powershell'
|
||||||
|
|
||||||
|
env = dict()
|
||||||
|
|
||||||
|
# We're being overly cautious about which keys to accept (more so than
|
||||||
|
# the Windows environment is capable of doing), since the powershell
|
||||||
|
# env provider's limitations don't appear to be documented.
|
||||||
|
safe_envkey = re.compile(r'^[\d\w_]{1,255}$')
|
||||||
|
|
||||||
|
def assert_safe_env_key(self, key):
|
||||||
|
if not self.safe_envkey.match(key):
|
||||||
|
raise AnsibleError("Invalid PowerShell environment key: %s" % key)
|
||||||
|
return key
|
||||||
|
|
||||||
|
def safe_env_value(self, key, value):
|
||||||
|
if len(value) > 32767:
|
||||||
|
raise AnsibleError("PowerShell environment value for key '%s' exceeds 32767 characters in length" % key)
|
||||||
|
# powershell single quoted literals need single-quote doubling as their only escaping
|
||||||
|
value = value.replace("'", "''")
|
||||||
|
return text_type(value)
|
||||||
|
|
||||||
def env_prefix(self, **kwargs):
|
def env_prefix(self, **kwargs):
|
||||||
return ''
|
env = self.env.copy()
|
||||||
|
env.update(kwargs)
|
||||||
|
return ';'.join(["$env:%s='%s'" % (self.assert_safe_env_key(k), self.safe_env_value(k,v)) for k,v in env.items()])
|
||||||
|
|
||||||
def join_path(self, *args):
|
def join_path(self, *args):
|
||||||
parts = []
|
parts = []
|
||||||
|
@ -156,6 +179,7 @@ class ShellModule(object):
|
||||||
Try
|
Try
|
||||||
{
|
{
|
||||||
%s
|
%s
|
||||||
|
%s
|
||||||
}
|
}
|
||||||
Catch
|
Catch
|
||||||
{
|
{
|
||||||
|
@ -186,7 +210,7 @@ class ShellModule(object):
|
||||||
Echo $_obj | ConvertTo-Json -Compress -Depth 99
|
Echo $_obj | ConvertTo-Json -Compress -Depth 99
|
||||||
Exit 1
|
Exit 1
|
||||||
}
|
}
|
||||||
''' % (' '.join(cmd_parts))
|
''' % (env_string, ' '.join(cmd_parts))
|
||||||
if rm_tmp:
|
if rm_tmp:
|
||||||
rm_tmp = self._escape(self._unquote(rm_tmp))
|
rm_tmp = self._escape(self._unquote(rm_tmp))
|
||||||
rm_cmd = 'Remove-Item "%s" -Force -Recurse -ErrorAction SilentlyContinue' % rm_tmp
|
rm_cmd = 'Remove-Item "%s" -Force -Recurse -ErrorAction SilentlyContinue' % rm_tmp
|
||||||
|
|
Loading…
Reference in a new issue