Run a script with a space in its path?

I’m trying to run a (PowerShell) script with a path in its name:


- name: Run a script
script: '"./files/name of a script.ps1" -Param1 {{ variable_name }}'

The above is only the latest attempting at quoting/escaping I’ve tried. Regardless, I get an error like this when I run the playbook containing the above task:


TASK [Run a script] *****************************
task path: /etc/ansible/playbook.yml:38
<192.168.200.245> ESTABLISH WINRM CONNECTION FOR USER: SOME-HOST\Administrator on PORT 5986 TO 192.168.200.245
<192.168.200.245> EXEC Set-StrictMode -Version Latest
(New-Item -Type Directory -Path $env:temp -Name "ansible-tmp-1466538051.8-95578181041077").FullName | Write-Host -Separator '';

<192.168.200.245> PUT "/etc/ansible/"./files/name\" TO "C:\Users\Administrator\AppData\Local\Temp\ansible-tmp-1466537998.0-243829631467080\name"
fatal: [some-host]: FAILED! => {"failed": true, "msg": "file or module does not exist: \"/etc/ansible/\"./files/name\\""}

Any ideas?

So just so we are clear… script attempts to pick .ps1 off your ansible controller and transfer it to the windows node before running it. If you want to run a .ps1 that is already there right now your only choice is to use raw.

If your script task is in a role then ‘role magic’ (as I have just decided to call it) will find files in the files subdir of your role without needing a relative path.

So like this ought to at least stop it looking in /etc/ansible.files\name… like it is at the moment.


- name: Run a script
script: name of a script.ps1 -Param1 {{ variable_name }}

You might need to escape the spaces in the file name


- name: Run a script
script: name\ of\ a\ script.ps1 -Param1 {{ variable_name }}

However, if at all possible, I’d recommend saving yourself some pain and renaming the .ps1 to avoid spaces, because powershell is very easily confused when it comes to parameter parsing (especially when calling external programs from within powershell).

Better still, convert the powershell into an ansible module if at all possible. That way you can do change detection in the powershell (script will allways report ‘changed’). Its really not that much more work to make a module - the only extra things are handling the module parameters and setting up the contents of the object that contains the outcome of the module.

Hope this helps,

Jon

Hi Jon,

I’m just starting out with Ansible so I haven’t created any roles, nor am I using any that anyone else created.

The file is in the /etc/ansible/files directory.

Escaping the spaces didn’t work. And it definitely seems like it’s Ansible itself that fails to parse the spaces, at least based on the error message.

I tried changing script to this:

script: ./files/name\ of\ a\ script.ps1 -Param1 {{ variable_name }}

But I get the same error as before.

I got it to work by renaming the script and replacing the spaces with underscores; the updated script:

script: "./files/name_of_a_script.ps1 -Param1 {{ variable_name }}"

It also worked without quoting:

script: ./files/name_of_a_script.ps1 -Param1 {{ variable_name }}

I’ll take a look at creating a module for this. That’d certainly be a little cleaner than adding an additional task to save some info to a register variable and use that to implement a custom changed_when setting for this task.

But just for clarification – should the script module support scripts with spaces in their path on Windows hosts?

Thanks,
Kenny

Looking at the integration tests for the script module, which you can find here, https://github.com/ansible/ansible/blob/devel/test/integration/roles/test_win_script/tasks/main.yml there aren’t any tests which demonstrate spaces in the file name, so I think its a case of it being something that’s not expected. Fortunately being an open source project anyone who is troubled by it can dig in and propose an enhancement.

I’d strongly recommend having a look at roles as they are a great ‘unit of reuse’. I prefer modules to be small and do one thing well as that makes them much more re-usable and composable with other modules - but ultimately it depends on what you need to achieve.

Hope this helps,

Jon

I’m assuming you’re the one who asked this on StackOverflow recently…

Please file a bug on this under the ansible/ansible project- it looks like it’s not a Windows-specific issue, given where it’s failing. No guarantees we’ll be able to thread the needle correctly for separating the script name from the args in the free-form script arg, but others have asked for a more explicit separation between script name and args in our script action, and this would be an excellent case for it.

Thanks,

-Matt

Thanks Jon. I’ll take a look at getting setup soonish so as to be able contribute to (or at least test) any changes for this.

And I am totally going to look into roles. In fact, the playbook I’m working on now – my very first playbook too – is a perfect candidate for a role. I’m also going to take a look at creating modules for my scripts as that’s also a nice way to encapsulate ‘atomic’ changes in playbooks.

Yes, Matt, I also asked this on Stack Overflow. Sorry for not being clear initially if you were the person that answered before.

I created a ‘feature idea’ issue for this on the ansible/ansible project on GitHub.

Thanks,
Kenny