From d0c49144691a35c192d5a144c61e39fbb3b48f38 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Mon, 17 Jun 2019 05:14:58 +1000 Subject: [PATCH] win_shell - fix space escaping for custom executable (#57455) --- changelogs/fragments/win_shell-arg-space.yaml | 2 ++ lib/ansible/modules/windows/win_shell.ps1 | 2 +- .../targets/win_shell/tasks/main.yml | 33 +++++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/win_shell-arg-space.yaml diff --git a/changelogs/fragments/win_shell-arg-space.yaml b/changelogs/fragments/win_shell-arg-space.yaml new file mode 100644 index 00000000000..9dd3943b24c --- /dev/null +++ b/changelogs/fragments/win_shell-arg-space.yaml @@ -0,0 +1,2 @@ +bugfixes: +- win_shell - Fix bug when setting ``args.executable`` to a pace with a space diff --git a/lib/ansible/modules/windows/win_shell.ps1 b/lib/ansible/modules/windows/win_shell.ps1 index f0542a8e211..d31fbee39d8 100644 --- a/lib/ansible/modules/windows/win_shell.ps1 +++ b/lib/ansible/modules/windows/win_shell.ps1 @@ -93,7 +93,7 @@ Else { $exec_args = "/c $raw_command_line" } -$command = "$exec_application $exec_args" +$command = "`"$exec_application`" $exec_args" $run_command_arg = @{ command = $command } diff --git a/test/integration/targets/win_shell/tasks/main.yml b/test/integration/targets/win_shell/tasks/main.yml index eb388b5b11a..490caff0ee0 100644 --- a/test/integration/targets/win_shell/tasks/main.yml +++ b/test/integration/targets/win_shell/tasks/main.yml @@ -269,7 +269,7 @@ - no_profile is changed - no_profile.cmd == "[System.Environment]::CommandLine" - no_profile.rc == 0 - - no_profile.stdout is match ('^powershell.exe -noninteractive -encodedcommand') + - no_profile.stdout is match ('^"powershell.exe" -noninteractive -encodedcommand') - name: execute powershell with no_profile win_shell: '[System.Environment]::CommandLine' @@ -284,4 +284,33 @@ - no_profile is changed - no_profile.cmd == "[System.Environment]::CommandLine" - no_profile.rc == 0 - - no_profile.stdout is match ('^powershell.exe -noprofile -noninteractive -encodedcommand') + - no_profile.stdout is match ('^"powershell.exe" -noprofile -noninteractive -encodedcommand') + +- name: create symbolic link with space in the path + win_command: cmd.exe /c mklink /d "C:\ansible test link" C:\Windows\System32\WindowsPowerShell\v1.0 + args: + creates: C:\ansible test link + +- block: + - name: run with space in the executable path + win_shell: '[System.Environment]::CommandLine' + args: + executable: C:\ansible test link\powershell + register: space_exe + + - debug: + var: space_exe.stdout|trim + + - name: assert run with space in the executable path + assert: + that: + - space_exe is successful + - space_exe is changed + - space_exe.cmd == '[System.Environment]::CommandLine' + - space_exe.rc == 0 + - space_exe.stdout|trim == '"C:\\ansible test link\\powershell.exe" /c [System.Environment]::CommandLine' + always: + - name: remove test symbolic link + win_file: + path: C:\ansible test link + state: absent