diff options
author | Jordan Borean <jborean93@gmail.com> | 2018-11-14 06:08:27 +1000 |
---|---|---|
committer | Matt Davis <nitzmahone@users.noreply.github.com> | 2018-11-13 12:08:27 -0800 |
commit | 4fc013a7fa36475627797a86a306b06a6981817c (patch) | |
tree | ceb67f624475bb9ae7e78aff78425b2a9c6ac848 /lib/ansible/executor | |
parent | 34fc66185e8f62f784c5d14233d504a00b3e5a05 (diff) | |
download | ansible-4fc013a7fa36475627797a86a306b06a6981817c.tar.gz |
powershell - fix async for Ansible.Basic (#48404)
* powershell - fix async for Ansible.Basic
* moved old out outside of the try block
Diffstat (limited to 'lib/ansible/executor')
-rw-r--r-- | lib/ansible/executor/powershell/module_wrapper.ps1 | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/lib/ansible/executor/powershell/module_wrapper.ps1 b/lib/ansible/executor/powershell/module_wrapper.ps1 index 84f30d8b18..f199e52274 100644 --- a/lib/ansible/executor/powershell/module_wrapper.ps1 +++ b/lib/ansible/executor/powershell/module_wrapper.ps1 @@ -93,7 +93,15 @@ foreach ($script in $Scripts) { } Write-AnsibleLog "INFO - start module exec with Invoke() - $ModuleName" "module_wrapper" + +# temporarily override the stdout stream and create our own in a StringBuilder +# we use this to ensure there's always an Out pipe and that we capture the +# output for things like async or psrp +$orig_out = [System.Console]::Out +$sb = New-Object -TypeName System.Text.StringBuilder +$new_out = New-Object -TypeName System.IO.StringWriter -ArgumentList $sb try { + [System.Console]::SetOut($new_out) $module_output = $ps.Invoke() } catch { # uncaught exception while executing module, present a prettier error for @@ -102,6 +110,9 @@ try { -ErrorRecord $_.Exception.InnerException.ErrorRecord $host.SetShouldExit(1) return +} finally { + [System.Console]::SetOut($orig_out) + $new_out.Dispose() } # other types of errors may not throw an exception in Invoke but rather just @@ -114,19 +125,11 @@ if ($ps.InvocationStateInfo.State -eq "Failed" -and $ModuleName -ne "script") { } Write-AnsibleLog "INFO - module exec ended $ModuleName" "module_wrapper" -$ansible_output = $ps.Runspace.SessionStateProxy.GetVariable("_ansible_output") - -# _ansible_output is a special var used by new modules to store the -# output JSON. If set, we consider the ExitJson and FailJson methods -# called and assume it contains the JSON we want and the pipeline -# output won't contain anything of note -# TODO: should we validate it or use a random variable name? -# TODO: should we use this behaviour for all new modules and not just -# ones running under psrp -if ($null -ne $ansible_output) { - Write-AnsibleLog "INFO - using the _ansible_output variable for module output - $ModuleName" "module_wrapper" - Write-Output -InputObject $ansible_output.ToString() -} elseif ($module_output.Count -gt 0) { +$stdout = $sb.ToString() +if ($stdout) { + Write-Output -InputObject $stdout +} +if ($module_output.Count -gt 0) { # do not output if empty collection Write-AnsibleLog "INFO - using the output stream for module output - $ModuleName" "module_wrapper" Write-Output -InputObject ($module_output -join "`r`n") |