summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Borean <jborean93@gmail.com>2019-09-19 06:53:56 +1000
committerGitHub <noreply@github.com>2019-09-19 06:53:56 +1000
commitbad338aca6e1434fabc277b882d421f6b6b89617 (patch)
tree87a3d86d033bdeb9c2b4863372117ba9c1439351
parentbf5b6695eccc621449a3307307af750ebdd135de (diff)
downloadansible-bad338aca6e1434fabc277b882d421f6b6b89617.tar.gz
win_exec_wrapper - Be more defensive when trying to get output errors (#62376)
* win_exec_wrapper - Be more defensive when trying to get output errors * Fix up property search
-rw-r--r--changelogs/fragments/win_exec-error.yaml2
-rw-r--r--lib/ansible/executor/powershell/module_wrapper.ps138
2 files changed, 36 insertions, 4 deletions
diff --git a/changelogs/fragments/win_exec-error.yaml b/changelogs/fragments/win_exec-error.yaml
new file mode 100644
index 0000000000..c2608ae86a
--- /dev/null
+++ b/changelogs/fragments/win_exec-error.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+- win_exec_wrapper - Be more defensive when it comes to getting unhandled exceptions
diff --git a/lib/ansible/executor/powershell/module_wrapper.ps1 b/lib/ansible/executor/powershell/module_wrapper.ps1
index 0ed44d28bd..b457515318 100644
--- a/lib/ansible/executor/powershell/module_wrapper.ps1
+++ b/lib/ansible/executor/powershell/module_wrapper.ps1
@@ -128,8 +128,22 @@ try {
} catch {
# uncaught exception while executing module, present a prettier error for
# Ansible to parse
- Write-AnsibleError -Message "Unhandled exception while executing module" `
- -ErrorRecord $_.Exception.InnerException.ErrorRecord
+ $error_params = @{
+ Message = "Unhandled exception while executing module"
+ ErrorRecord = $_
+ }
+
+ # Be more defensive when trying to find the InnerException in case it isn't
+ # set. This shouldn't ever be the case but if it is then it makes it more
+ # difficult to track down the problem.
+ if ($_.Exception.PSObject.Properties.Name -contains "InnerException") {
+ $inner_exception = $_.Exception.InnerException
+ if ($inner_exception.PSObject.Properties.Name -contains "ErrorRecord") {
+ $error_params.ErrorRecord = $inner_exception.ErrorRecord
+ }
+ }
+
+ Write-AnsibleError @error_params
$host.SetShouldExit(1)
return
} finally {
@@ -140,8 +154,24 @@ try {
# other types of errors may not throw an exception in Invoke but rather just
# set the pipeline state to failed
if ($ps.InvocationStateInfo.State -eq "Failed" -and $ModuleName -ne "script") {
- Write-AnsibleError -Message "Unhandled exception while executing module" `
- -ErrorRecord $ps.InvocationStateInfo.Reason.ErrorRecord
+ $reason = $ps.InvocationStateInfo.Reason
+ $error_params = @{
+ Message = "Unhandled exception while executing module"
+ }
+
+ # The error record should always be set on the reason but this does not
+ # always happen on Server 2008 R2 for some reason (probably memory hotfix).
+ # Be defensive when trying to get the error record and fall back to other
+ # options.
+ if ($null -eq $reason) {
+ $error_params.Message += ": Unknown error"
+ } elseif ($reason.PSObject.Properties.Name -contains "ErrorRecord") {
+ $error_params.ErrorRecord = $reason.ErrorRecord
+ } else {
+ $error_params.Message += ": $($reason.ToString())"
+ }
+
+ Write-AnsibleError @error_params
$host.SetShouldExit(1)
return
}