diff --git a/lib/ansible/plugins/action/script.py b/lib/ansible/plugins/action/script.py index 3549e233212..d37a351a555 100644 --- a/lib/ansible/plugins/action/script.py +++ b/lib/ansible/plugins/action/script.py @@ -22,8 +22,9 @@ import re import shlex from ansible.errors import AnsibleError -from ansible.module_utils._text import to_native, to_text +from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.plugins.action import ActionBase +from ansible.plugins.shell.powershell import exec_wrapper class ActionModule(ActionBase): @@ -118,7 +119,12 @@ class ActionModule(ActionBase): exec_data = None # HACK: come up with a sane way to pass around env outside the command if self._connection.transport == "winrm": - exec_data = self._connection._create_raw_wrapper_payload(script_cmd, env_dict) + pay = self._connection._create_raw_wrapper_payload(script_cmd, + env_dict) + exec_data = exec_wrapper.replace(b"$json_raw = ''", + b"$json_raw = @'\r\n%s\r\n'@" + % to_bytes(pay)) + script_cmd = "-" result.update(self._low_level_execute_command(cmd=script_cmd, in_data=exec_data, sudoable=True, chdir=chdir)) diff --git a/lib/ansible/plugins/connection/winrm.py b/lib/ansible/plugins/connection/winrm.py index 1ec9486e409..ccbf52e34fe 100644 --- a/lib/ansible/plugins/connection/winrm.py +++ b/lib/ansible/plugins/connection/winrm.py @@ -295,7 +295,7 @@ class Connection(ConnectionBase): except Exception as ex: from traceback import format_exc - display.warning("FATAL ERROR DURING FILE TRANSFER: %s" % format_exc(ex)) + display.warning("FATAL ERROR DURING FILE TRANSFER: %s" % to_text(ex)) stdin_push_failed = True if stdin_push_failed: diff --git a/lib/ansible/plugins/shell/powershell.py b/lib/ansible/plugins/shell/powershell.py index 5e31389ff40..0d4fada58cc 100644 --- a/lib/ansible/plugins/shell/powershell.py +++ b/lib/ansible/plugins/shell/powershell.py @@ -35,7 +35,6 @@ if _powershell_version: _common_args = ['PowerShell', '-Version', _powershell_version] + _common_args[1:] exec_wrapper = br''' -#Requires -Version 3.0 begin { $DebugPreference = "Continue" $ErrorActionPreference = "Stop" @@ -566,7 +565,6 @@ namespace Ansible.Shell "@ $exec_wrapper = { -#Requires -Version 3.0 $DebugPreference = "Continue" $ErrorActionPreference = "Stop" Set-StrictMode -Version 2 @@ -835,7 +833,6 @@ $ErrorActionPreference = "Stop" # return asyncresult to controller $exec_wrapper = { -#Requires -Version 3.0 $DebugPreference = "Continue" $ErrorActionPreference = "Stop" Set-StrictMode -Version 2 diff --git a/test/integration/targets/win_script/tasks/main.yml b/test/integration/targets/win_script/tasks/main.yml index 32b637630af..78fa438d143 100644 --- a/test/integration/targets/win_script/tasks/main.yml +++ b/test/integration/targets/win_script/tasks/main.yml @@ -54,6 +54,24 @@ - "not test_script_with_args_result | failed" - "test_script_with_args_result | changed" +# Bug: https://github.com/ansible/ansible/issues/32850 +- name: set fact of long string + set_fact: + long_string: "{{ lookup('pipe', 'printf \"a%.0s\" {1..1000}') }}" + +- name: run test script with args that exceed the stdin buffer + script: test_script_with_args.ps1 {{ long_string }} + register: test_script_with_large_args_result + +- name: check that script ran and received arguments correctly + assert: + that: + - test_script_with_large_args_result.rc == 0 + - test_script_with_large_args_result.stdout == long_string + "\r\n" + - not test_script_with_large_args_result.stderr + - test_script_with_large_args_result is not failed + - test_script_with_large_args_result is changed + - name: run test script that takes parameters passed via splatting script: test_script_with_splatting.ps1 @{ This = 'this'; That = '{{ test_win_script_value }}'; Other = 'other'} register: test_script_with_splatting_result @@ -201,20 +219,17 @@ - "test_script_bool_result.stdout_lines[0] == 'System.Boolean'" - "test_script_bool_result.stdout_lines[1] == 'True'" -# FIXME: re-enable this test once script can run under the wrapper with powershell -#- name: run test script that uses envvars -# script: test_script_with_env.ps1 -# environment: -# taskenv: task -# register: test_script_env_result -# -#- name: ensure that script ran and that environment var was passed -# assert: -# that: -# - test_script_env_result | succeeded -# - test_script_env_result.stdout_lines[0] == 'task' -# +- name: run test script that uses envvars + script: test_script_with_env.ps1 + environment: + taskenv: task + register: test_script_env_result +- name: ensure that script ran and that environment var was passed + assert: + that: + - test_script_env_result | succeeded + - test_script_env_result.stdout_lines[0] == 'task' # check mode - name: Run test script that creates a file in check mode