Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle PowerShell parameters passed via splatting #9602

Merged
merged 1 commit into from
Feb 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ansible/runner/connection_plugins/winrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def exec_command(self, cmd, tmp_path, sudo_user=None, sudoable=False, executable
vvv("EXEC %s" % cmd, host=self.host)
# For script/raw support.
if cmd_parts and cmd_parts[0].lower().endswith('.ps1'):
script = powershell._build_file_cmd(cmd_parts)
script = powershell._build_file_cmd(cmd_parts, quote_args=False)
cmd_parts = powershell._encode_script(script, as_list=True)
try:
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:], from_exec=True)
Expand Down
8 changes: 5 additions & 3 deletions lib/ansible/runner/shell_plugins/powershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ def _encode_script(script, as_list=False):
return cmd_parts
return ' '.join(cmd_parts)

def _build_file_cmd(cmd_parts):
def _build_file_cmd(cmd_parts, quote_args=True):
'''Build command line to run a file, given list of file name plus args.'''
return ' '.join(_common_args + ['-ExecutionPolicy', 'Unrestricted', '-File'] + ['"%s"' % x for x in cmd_parts])
if quote_args:
cmd_parts = ['"%s"' % x for x in cmd_parts]
return ' '.join(['&'] + cmd_parts)

class ShellModule(object):

Expand Down Expand Up @@ -110,7 +112,7 @@ def build_module_command(self, env_string, shebang, cmd, rm_tmp=None):
cmd_parts = shlex.split(cmd, posix=False)
if not cmd_parts[0].lower().endswith('.ps1'):
cmd_parts[0] = '%s.ps1' % cmd_parts[0]
script = _build_file_cmd(cmd_parts)
script = _build_file_cmd(cmd_parts, quote_args=False)
if rm_tmp:
rm_tmp = _escape(rm_tmp)
script = '%s; Remove-Item "%s" -Force -Recurse;' % (script, rm_tmp)
Expand Down
5 changes: 5 additions & 0 deletions test/integration/roles/test_win_script/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

# Parameters to pass to test scripts.
test_win_script_value: VaLuE
test_win_script_splat: "@{This='THIS'; That='THAT'; Other='OTHER'}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Test script to make sure the Ansible script module works when arguments are
# passed via splatting (http://technet.microsoft.com/en-us/magazine/gg675931.aspx)

Write-Host $args.This
Write-Host $args.That
Write-Host $args.Other
32 changes: 32 additions & 0 deletions test/integration/roles/test_win_script/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,38 @@
- "not test_script_with_args_result|failed"
- "test_script_with_args_result|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

- name: check that script ran and received parameters via splatting
assert:
that:
- "test_script_with_splatting_result.rc == 0"
- "test_script_with_splatting_result.stdout"
- "test_script_with_splatting_result.stdout_lines[0] == 'this'"
- "test_script_with_splatting_result.stdout_lines[1] == test_win_script_value"
- "test_script_with_splatting_result.stdout_lines[2] == 'other'"
- "not test_script_with_splatting_result.stderr"
- "not test_script_with_splatting_result|failed"
- "test_script_with_splatting_result|changed"

- name: run test script that takes splatted parameters from a variable
script: test_script_with_splatting.ps1 {{ test_win_script_splat|quote }}
register: test_script_with_splatting2_result

- name: check that script ran and received parameters via splatting from a variable
assert:
that:
- "test_script_with_splatting2_result.rc == 0"
- "test_script_with_splatting2_result.stdout"
- "test_script_with_splatting2_result.stdout_lines[0] == 'THIS'"
- "test_script_with_splatting2_result.stdout_lines[1] == 'THAT'"
- "test_script_with_splatting2_result.stdout_lines[2] == 'OTHER'"
- "not test_script_with_splatting2_result.stderr"
- "not test_script_with_splatting2_result|failed"
- "test_script_with_splatting2_result|changed"

- name: run test script that has errors
script: test_script_with_errors.ps1
register: test_script_with_errors_result
Expand Down