summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Davis <mdavis@ansible.com>2017-04-06 01:45:04 -0700
committerMatt Davis <mdavis@ansible.com>2017-04-06 01:45:04 -0700
commitbeb40dd10e06ce9bc538f4a29efcf65332a511a6 (patch)
treec39ac0925cfc8c5500c6a78e316c7a3aed104e6b
parentefb717fe73139e5fe5e3aa957e1aa1fbb6cf41f9 (diff)
downloadansible-beb40dd10e06ce9bc538f4a29efcf65332a511a6.tar.gz
Cleanse CLIXML from win_shell stderr (#23329)
prunes all but Error stream values cherry-picked from b71b157fb3c9816959cdf7a020b498500ce25f33 (plus fixed merge conflicts due to devel cleanup)
-rw-r--r--lib/ansible/modules/windows/win_shell.ps154
1 files changed, 43 insertions, 11 deletions
diff --git a/lib/ansible/modules/windows/win_shell.ps1 b/lib/ansible/modules/windows/win_shell.ps1
index 80742f032d..af99612c31 100644
--- a/lib/ansible/modules/windows/win_shell.ps1
+++ b/lib/ansible/modules/windows/win_shell.ps1
@@ -60,22 +60,54 @@ namespace Ansible.Shell
}
"@
-$parsed_args = Parse-Args $args $false
+# Cleanse CLIXML from stderr (sift out error stream data, discard others for now)
+Function Cleanse-Stderr($raw_stderr) {
+ Try {
+ # NB: this regex isn't perfect, but is decent at finding CLIXML amongst other stderr noise
+ If($raw_stderr -match "(?s)(?<prenoise1>.*)#< CLIXML(?<prenoise2>.*)(?<clixml><Objs.+</Objs>)(?<postnoise>.*)") {
+ $clixml = [xml]$matches["clixml"]
+
+ $merged_stderr = "{0}{1}{2}{3}" -f @(
+ $matches["prenoise1"],
+ $matches["prenoise2"],
+ # filter out just the Error-tagged strings for now, and zap embedded CRLF chars
+ ($clixml.Objs.ChildNodes | ? { $_.Name -eq 'S' } | ? { $_.S -eq 'Error' } | % { $_.'#text'.Replace('_x000D__x000A_','') } | Out-String),
+ $matches["postnoise"]) | Out-String
+
+ return $merged_stderr.Trim()
+
+ # FUTURE: parse/return other streams
+ }
+ Else {
+ $raw_stderr
+ }
+ }
+ Catch {
+ "***EXCEPTION PARSING CLIXML: $_***" + $raw_stderr
+ }
+}
+
+$params = Parse-Args $args -supports_check_mode $false
-$raw_command_line = $(Get-AnsibleParam $parsed_args "_raw_params" -failifempty $true).Trim()
-$chdir = Get-AnsibleParam $parsed_args "chdir" -type "path"
-$executable = Get-AnsibleParam $parsed_args "executable" -type "path"
-$creates = Get-AnsibleParam $parsed_args "creates" -type "path"
-$removes = Get-AnsibleParam $parsed_args "removes" -type "path"
+$raw_command_line = Get-AnsibleParam -obj $params -name "_raw_params" -type "str" -failifempty $true
+$chdir = Get-AnsibleParam -obj $params -name "chdir" -type "path"
+$executable = Get-AnsibleParam -obj $params -name "executable" -type "path"
+$creates = Get-AnsibleParam -obj $params -name "creates" -type "path"
+$removes = Get-AnsibleParam -obj $params -name "removes" -type "path"
-$result = @{changed=$true; warnings=@(); cmd=$raw_command_line}
+$raw_command_line = $raw_command_line.Trim()
+
+$result = @{
+ changed = $true
+ cmd = $raw_command_line
+}
If($creates -and $(Test-Path $creates)) {
- Exit-Json @{cmd=$raw_command_line; msg="skipped, since $creates exists"; changed=$false; skipped=$true; rc=0}
+ Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
}
If($removes -and -not $(Test-Path $removes)) {
- Exit-Json @{cmd=$raw_command_line; msg="skipped, since $removes does not exist"; changed=$false; skipped=$true; rc=0}
+ Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
}
Add-Type -TypeDefinition $helper_def
@@ -120,7 +152,7 @@ Catch [System.ComponentModel.Win32Exception] {
# fail nicely for "normal" error conditions
# FUTURE: this probably won't work on Nano Server
$excep = $_
- Exit-Json @{failed=$true;changed=$false;cmd=$raw_command_line;rc=$excep.Exception.NativeErrorCode;msg=$excep.Exception.Message}
+ Exit-Json @{msg = $excep.Exception.Message; cmd = $raw_command_line; changed = $false; rc = $excep.Exception.NativeErrorCode}
}
$stdout = $stderr = [string] $null
@@ -128,7 +160,7 @@ $stdout = $stderr = [string] $null
[Ansible.Shell.ProcessUtil]::GetProcessOutput($proc.StandardOutput, $proc.StandardError, [ref] $stdout, [ref] $stderr) | Out-Null
$result.stdout = $stdout
-$result.stderr = $stderr
+$result.stderr = Cleanse-Stderr $stderr
# TODO: decode CLIXML stderr output (and other streams?)