win: handle non string as an environment value (#37215)

* win: handle non string as an environment value

* Changed powershell environment handler to use .net function instead for special chars
This commit is contained in:
Jordan Borean 2018-03-09 08:02:54 +10:00 committed by GitHub
parent 58261a42e9
commit 708869edd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View file

@ -162,7 +162,10 @@ Function Run($payload) {
$ps.AddStatement().AddScript("Function Write-Host(`$msg){ Write-Output `$msg }") | Out-Null
ForEach ($env_kv in $payload.environment.GetEnumerator()) {
$escaped_env_set = "`$env:{0} = '{1}'" -f $env_kv.Key,$env_kv.Value.Replace("'","''")
# need to escape ' in both the key and value
$env_key = $env_kv.Key.ToString().Replace("'", "''")
$env_value = $env_kv.Value.ToString().Replace("'", "''")
$escaped_env_set = "[System.Environment]::SetEnvironmentVariable('{0}', '{1}')" -f $env_key, $env_value
$ps.AddStatement().AddScript($escaped_env_set) | Out-Null
}

View file

@ -31,6 +31,32 @@
- invalid_ps_version is failed
- '"This module cannot run as it requires a minimum PowerShell version of 20.0.0.0, actual was " in invalid_ps_version.msg'
- name: test out environment block for task
win_shell: set | more
args:
executable: cmd.exe
environment:
String: string value
Int: 1234
Bool: True
double_quote: 'double " quote'
single_quote: "single ' quote"
hyphen-var: abc@123
'_-(){}[]<>*+-/\?"''!@#$%^&|;:i,.`~0': '_-(){}[]<>*+-/\?"''!@#$%^&|;:i,.`~0'
register: environment_block
- name: assert environment block for task
assert:
that:
- '"String=string value" in environment_block.stdout_lines'
- '"Int=1234" in environment_block.stdout_lines'
- '"Bool=True" in environment_block.stdout_lines'
- '"double_quote=double \" quote" in environment_block.stdout_lines'
- '"single_quote=single '' quote" in environment_block.stdout_lines'
- '"hyphen-var=abc@123" in environment_block.stdout_lines'
# yaml escaping rules - (\\ == \), (\" == "), ('' == ')
- '"_-(){}[]<>*+-/\\?\"''!@#$%^&|;:i,.`~0=_-(){}[]<>*+-/\\?\"''!@#$%^&|;:i,.`~0" in environment_block.stdout_lines'
- name: test out become requires without become_user set
test_all_options:
register: become_system