psrp - fix up script chdir setting

pull/81282/head
Jordan Borean 11 months ago
parent 894c339df8
commit 174b964829
No known key found for this signature in database
GPG Key ID: 2AAC89085FBBDAB5

@ -0,0 +1,2 @@
bugfixes:
- psrp script - Support setting ``chdir`` with ``script`` when using the ``psrp`` connection plugin - https://github.com/ansible/ansible/issues/81277

@ -435,6 +435,25 @@ class Connection(ConnectionBase):
pwsh_in_data: bytes | str | None = None
# _low_level_execute_command can prepend a chdir value which would
# break the logic here to extract the PowerShell script. Store the
# chdir value if it is set and prepend it later on if present and the
# script that was extracted is our module payload.
original_cmd = cmd
change_dir_command = ''
shell_append_value = self._shell.append_command('', '')
if cmd.startswith('cd ') and shell_append_value in cmd:
change_dir_command, cmd = cmd.split(shell_append_value, 1)
# Windows exec wrapper runs in another runspace so we need to
# extract the new directory and use a different command than cd.
# _low_level_execute_command does not quote the value as well so
# we need to do so ourselves.
directory = change_dir_command[3:]
change_dir_command = f"cd '{directory}'; [System.Environment]::CurrentDirectory = '{directory}'"
script = None
if cmd.startswith(" ".join(_common_args) + " -EncodedCommand"):
# This is a PowerShell script encoded by the shell plugin, we will
# decode the script and execute it in the runspace instead of
@ -458,11 +477,21 @@ class Connection(ConnectionBase):
if bootstrap_wrapper == cmd:
# Do not display to the user each invocation of the bootstrap wrapper
display.vvv("PSRP: EXEC (via pipeline wrapper)")
if change_dir_command:
script = f"{change_dir_command}{shell_append_value}{script}"
elif change_dir_command:
# The cd was not added by _low_level_execute_command for a
# module run, fallback to running the orignal cmd.
pwsh_in_data = script = None
else:
display.vvv("PSRP: EXEC %s" % script, host=self._psrp_host)
else:
if not script:
# In other cases we want to execute the cmd as the script. We add on the 'exit $LASTEXITCODE' to ensure the
# rc is propagated back to the connection plugin.
cmd = original_cmd # Make sure we undo all the cd munging done before
script = to_text(u"%s\nexit $LASTEXITCODE" % cmd)
pwsh_in_data = in_data
display.vvv(u"PSRP: EXEC %s" % script, host=self._psrp_host)

@ -134,3 +134,37 @@
path: /tmp/empty.txt
state: absent
delegate_to: localhost
# https://github.com/ansible/ansible/issues/81277
# First few tests ensure the cd checking doesn't impact raw commands and only
# ones added by _low_level_execute_command
- name: run raw command with cd command without normal command separator
raw: cd C:\Windows; $pwd.Path
register: raw_cd1
- name: run raw command with cd command with normal command separator
raw: cd c:\Windows ; $pwd.Path
register: raw_cd2
- name: run raw command with common PowerShell encoded command wrapper
raw: cd c:\Windows ; PowerShell -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -EncodedCommand {{ '$pwd.Path' | b64encode(encoding='utf-16-le') }}
register: raw_cd3
- name: assert raw cd commands worked
assert:
that:
- raw_cd1.stdout == 'C:\\Windows'
- raw_cd2.stdout == 'C:\\Windows'
- raw_cd3.stdout == 'C:\\Windows'
- name: run script with chdir
script: cd_script.ps1
args:
chdir: C:\Windows
register: script_cd
- name: assert script chdir output
assert:
that:
- script_cd.stdout

Loading…
Cancel
Save