I am trying to pass ansible variables to my powershell script as parameters. While they are successfully being passed, the path of my file is being cut short. For example, when passing a string '/Microsoft SQL Server/files/blah', and printing it in the code, it becomes '/Microsoft'.
Here is my playbook file:
- name: Deploy Report
hosts: ******
tasks:
- name: Run Powershell Script
script: "../deploy.ps1 -input1 '{{ input1 }}'"
args:
executable: powershell
Any help is greatly appreciated
Judging by the documentation of the Ansible
scriptmodule (and since confirmed to be correct by you):A given script and its arguments are executed via a shell.
On Windows that shell appears to be PowerShell (
powershell.exe, the Windows PowerShell CLI), and it appears that what follows thescript:property is passed to the-Command(-c) parameter.;For that reason, you do not need to specify an
executable:argument in order to execute PowerShell scripts on Windows, and the following should suffice (note that I'm using the module's FCQN (Fully Qualified Collection Name),ansible.builtin.scriptinstead of the short namescript:, as recommended in the docs):As for what you tried:
By specifying
powershellas anexecutable:argument underargs:, you ended up nesting two instances of PowerShell: one that is implicitly used for execution, and the other is the one you're calling explicitly.-Command(-c) nor-File(-f) before a command in apowershell.execall implies-Command(-c)This nesting is not only unnecessary, but causes the effective loss of the
'...'quoting in yourscript:command line, which explains your symptom.The following example demonstrates this - run it from an interactive
cmd.exesession (to simulate a no-shell invocation ofpowershell.exe; if you place-noexitbefore-c, you can also invoke the command from the WindowsRundialog (WinKey+R) for a true no-shell call):This doesn't output verbatim
foo bar, as one might expect, but outputsfooandbaron separate lines; that is, the'...'enclosure was effectively lost, and what was ultimately executed wasWrite-Output foo bar, i.e, two arguments were passed.The reasons are subtle:
When PowerShell executes an external program - including its own CLI - it must translate its
'...'quoting into"..."quoting behind the scenes, given that Windows CLIs can only be expected to understand the latter form.Therefore, when the outer
powershellcall invokes the inner one, it translates'foo bar'into"foo bar"behind the scenes.However,
powershell.execonsiders (unescaped)"..."quoting to merely have syntactic function on the command line, and such quoting is removed before the resulting argument(s) are interpreted as PowerShell code, so that the innerpowershell.exeends up executingWrite-Output foo barIf you use
"..."quoting to begin with, you can see the problem even with a single call topowershell.exe(again,fooandbarprint on separate lines, due to two arguments getting passed):[1] Notably, this default changed to
-File(-f) inpwsh, the PowerShell (Core) 7+ CLI.