[PR #11308/4b67afc2 backport][stable-12] Add option for wsl_shell_type, protect wsl.exe arguments if SSH shell is Powershell (#11433)

Add option for wsl_shell_type, protect wsl.exe arguments if SSH shell is Powershell (#11308)

* feat(wsl): add option for wsl_shell_type, protect wsl arguments if SSH shell is Powershell

* docs(wsl): add changelog fragment

* docs(wsl): fix changelog fragment syntax, add issue link



* feat(wsl): improve new option documentation



* refactor(wsl): put integrasion test flag into a variable for convenience

* feat(wsl): rename option to wsl_remote_ssh_shell_type

* feat(wsl): escape "%" if shell is cmd, raise AnsibleError if powershell

* test(wsl): fix unit tests for wsl

- remove redundant check - moved to a separate function
- fix check for cmd escaping of "%"
- fix formatting / whitespace

* test(wsl): fix expected error message

* test(wsl): fix test - position of stop-parsing token changed



---------


(cherry picked from commit 4b67afc2b0)

Co-authored-by: fizmat <fizmat.r66@gmail.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
patchback[bot]
2026-01-16 21:26:29 +01:00
committed by GitHub
parent 68b2385efd
commit 5b571fd53f
3 changed files with 45 additions and 3 deletions

View File

@@ -210,6 +210,19 @@ def test_build_wsl_command(connection):
assert cmd == 'wsl.exe --distribution test --user test-become-user -- /bin/sh -c "ls -la"'
def test_build_wsl_command_powershell(connection):
"""Test wsl command building for powershell and cmd remote ssh shell"""
cmd = connection._build_wsl_command('/bin/sh -c "ls -la %PATH%"')
assert cmd == 'wsl.exe --distribution test -- /bin/sh -c "ls -la ^%PATH^%"'
connection.set_option("wsl_remote_ssh_shell_type", "powershell")
cmd = connection._build_wsl_command('/bin/sh -c "ls -la"')
assert cmd == 'wsl.exe --% --distribution test -- /bin/sh -c "ls -la"'
with pytest.raises(AnsibleError, match="The command contains '%', cannot safely escape it for Powershell"):
connection._build_wsl_command('/bin/sh -c "ls -la %PATH%"')
@patch("paramiko.SSHClient")
def test_exec_command_success(mock_ssh, connection):
"""Test successful command execution"""