diff --git a/lib/ansible/plugins/shell/powershell.py b/lib/ansible/plugins/shell/powershell.py index 4299043dcad..c4e9d20b5aa 100644 --- a/lib/ansible/plugins/shell/powershell.py +++ b/lib/ansible/plugins/shell/powershell.py @@ -1124,6 +1124,15 @@ $ErrorActionPreference = "Stop" # return asyncresult to controller $exec_wrapper = { + # help to debug any errors in the exec_wrapper or async_watchdog by generating + # an error log in case of a terminating error + trap { + $log_path = "$($env:TEMP)\async-exec-wrapper-$(Get-Date -Format "yyyy-MM-ddTHH-mm-ss.ffffZ")-error.txt" + $error_msg = "Error while running the async exec wrapper`r`n$_`r`n$($_.ScriptStackTrace)" + Set-Content -Path $log_path -Value $error_msg + throw $_ + } + &chcp.com 65001 > $null $DebugPreference = "Continue" $ErrorActionPreference = "Stop" @@ -1201,9 +1210,6 @@ $exec_wrapper = { Function Run($payload) { $remote_tmp = $payload["module_args"]["_ansible_remote_tmp"] $remote_tmp = [System.Environment]::ExpandEnvironmentVariables($remote_tmp) - if ($null -eq $remote_tmp) { - $remote_tmp = $original_tmp - } # calculate the result path so we can include it in the worker payload $jid = $payload.async_jid @@ -1267,6 +1273,19 @@ Function Run($payload) { } $watchdog_pid = $process.ProcessId + # populate initial results before we send the async data to avoid result race + $result = @{ + started = 1; + finished = 0; + results_file = $results_path; + ansible_job_id = $local_jid; + _ansible_suppress_tmpdir_delete = $true; + ansible_async_watchdog_pid = $watchdog_pid + } + + $result_json = ConvertTo-Json $result + Set-Content $results_path -Value $result_json + # wait until the client connects, throw an error if the timeout is reached $wait_async = $pipe.BeginWaitForConnection($null, $null) $wait_async.AsyncWaitHandle.WaitOne(5000) > $null @@ -1283,19 +1302,6 @@ Function Run($payload) { $pipe.Close() } - # populate initial results before we resume the process to avoid result race - $result = @{ - started=1; - finished=0; - results_file=$results_path; - ansible_job_id=$local_jid; - _ansible_suppress_tmpdir_delete=$true; - ansible_async_watchdog_pid=$watchdog_pid - } - - $result_json = ConvertTo-Json $result - Set-Content $results_path -Value $result_json - return $result_json } diff --git a/test/integration/targets/win_async_wrapper/library/async_test.ps1 b/test/integration/targets/win_async_wrapper/library/async_test.ps1 index 63dbf86ca9a..fa41b3e8241 100644 --- a/test/integration/targets/win_async_wrapper/library/async_test.ps1 +++ b/test/integration/targets/win_async_wrapper/library/async_test.ps1 @@ -1,21 +1,8 @@ #!powershell -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . - -# WANT_JSON -# POWERSHELL_COMMON + +# Copyright: (c) 2018, Ansible Project + +#Requires -Module Ansible.ModuleUtils.Legacy $parsed_args = Parse-Args $args diff --git a/test/integration/targets/win_async_wrapper/tasks/main.yml b/test/integration/targets/win_async_wrapper/tasks/main.yml index 1615f76c233..f247aa8407a 100644 --- a/test/integration/targets/win_async_wrapper/tasks/main.yml +++ b/test/integration/targets/win_async_wrapper/tasks/main.yml @@ -4,7 +4,7 @@ - name: async fire and forget async_test: - sleep_delay_sec: 8 + sleep_delay_sec: 10 async: 20 poll: 0 register: asyncresult @@ -23,7 +23,7 @@ - name: async poll immediate success async_test: sleep_delay_sec: 0 - async: 20 + async: 10 poll: 1 register: asyncresult @@ -61,7 +61,7 @@ - name: async poll retry async_test: sleep_delay_sec: 5 - async: 20 + async: 10 poll: 1 register: asyncresult @@ -97,8 +97,8 @@ - name: async poll timeout async_test: - sleep_delay_sec: 25 - async: 20 + sleep_delay_sec: 5 + async: 3 poll: 1 register: asyncresult ignore_errors: true @@ -152,7 +152,7 @@ - name: echo some non ascii characters win_command: cmd.exe /c echo über den Fußgängerübergang gehen - async: 20 + async: 10 poll: 1 register: nonascii_output