It may be easier to use the short filenames but arguably you should avoid using the short filenames as it can be a security risk. You could inadvertently run the wrong or malicious executable if someone creates a file or folder in a particular way it could change what the ~1, ~2 actually point to.
I would highly recommend you stick with quoting the paths when using win_command like so
`
- name: run an executable inside a folder with a space
win_command: ‘“C:\Program Files\Microsoft Windows\some-exe.exe” /argument1 “/argument 2 with space” “/argument 3 with ‘’ single quote”’
or with chdir (note: you shouldn’t have to quote the chdir arg)
- name: run an executable with custom working dir
win_command: some-exe.exe /argument1 “/argument 2 with space”
args:
chdir: C:\Program Files\Microsoft Windows
`
It really just comes down to the fact that you should be quoting an argument with " (double quotes) to make sure it ignores spaces. In the first example, due to the YAML rules, I made sure the whole value is enclosed in ’ (single quotes) which means I only have to escape single quote instances by doubling it, e.g. ’ becomes ‘’.
The win_shell module is different as the quoting rules are dependent on the shell being used, here are some ways you can do it (note: chdir is still the same where you don’t have to quote the value if you use it
`
-
name: run an executable with a space (powershell)
win_shell: &“C:\Program Files\Microsoft Windows\some-exe.exe” @(“/argument1”, “/argument 2 with space”)
-
name: run an executable with a space (cmd)
win_shell: ‘“C:\Program Files\Microsoft Windows\some-exe.exe” /argument1 “/argument 2 with space”’
args:
executable: cmd.exe
`
Arguably you should be using win_command if all you are doing is running an executable as that is what it is for and win_shell is used when you want to run shell commands. You can still use win_shell to run executables and as you can see from the example above the cmd one is pretty much the same as win_command but it complicates it further as Windows parses values differently in each scenario. For example, the caret symbol (^) is seen as an escape character in cmd but not the native argument parser for Windows so you would have to handle them differently depending on what module you use.
Here is a short snippet showing you what happens when using the chdir argument, I’ll use win_shell as an example but the same would apply with win_command as well.
`
-
hosts: ‘2016’
gather_facts: no
tasks:
-
win_shell: pwd
args:
chdir: C:\Program Files
-
win_shell: cd
args:
executable: cmd.exe
chdir: C:\Program Files
`
and the output
TASK [win_shell] ********************************************************************************************************************************************
task path: /Users/jborean/dev/module-tester/adhoc.yml:4
Using module file /Users/jborean/dev/ansible/lib/ansible/modules/windows/win_shell.ps1
<SERVER2016.domain.local> ESTABLISH WINRM CONNECTION FOR USER: vagrant on PORT 5986 TO SERVER2016.domain.local
EXEC (via pipeline wrapper)
changed: [SERVER2016.domain.local] => {
“attempts”: 1,
“changed”: true,
“cmd”: “pwd”,
“delta”: “0:00:00.640518”,
“end”: “2018-01-07 08:41:58.701292”,
“rc”: 0,
“start”: “2018-01-07 08:41:58.060773”,
“stderr”: “”,
“stderr_lines”: ,
“stdout”: “\r\nPath \r\n---- \r\nC:\Program Files\r\n\r\n\r\n”,
“stdout_lines”: [
“”,
"Path ",
"---- ",
“C:\Program Files”,
“”,
“”
]
}
TASK [win_shell] ********************************************************************************************************************************************
task path: /Users/jborean/dev/module-tester/adhoc.yml:8
Using module file /Users/jborean/dev/ansible/lib/ansible/modules/windows/win_shell.ps1
<SERVER2016.domain.local> ESTABLISH WINRM CONNECTION FOR USER: vagrant on PORT 5986 TO SERVER2016.domain.local
EXEC (via pipeline wrapper)
changed: [SERVER2016.domain.local] => {
“attempts”: 1,
“changed”: true,
“cmd”: “cd”,
“delta”: “0:00:00.249102”,
“end”: “2018-01-07 08:41:59.919811”,
“rc”: 0,
“start”: “2018-01-07 08:41:59.670709”,
“stderr”: “”,
“stderr_lines”: ,
“stdout”: “C:\Program Files\r\n”,
“stdout_lines”: [
“C:\Program Files”
]
}
META: ran handlers
META: ran handlers
As you can see I didn’t have to quote the chdir argument when defining it in YAML and the current directory changed to “C:\Program Files” in both the powershell and cmd instances.
You can find more info here https://docs.ansible.com/ansible/devel/windows_usage.html#running-commands, the argument rules is probably the most relevant.
Thanks
Jordan