t.Hi,
I’m currently trying to use the win_command module of Ansible. Task (in a role, itself included in a playbook) looks like that :
`
- name: Test module
win_command: dir
`
That’s quite simple, really. Connection to the target works perfectly fine since I’ve been able to install features, and even run commands using the win_shell module.
But this particular task invariably fail with this error : (ansible -vvv)
`
fatal: [myvmFQDN]: FAILED! => {
“changed”: false,
“cmd”: “dir”,
“failed”: true,
“msg”: “The system cannot find the file specified”,
“rc”: 2
}
`
Using ansible -vvvvvv… does not help.
Target is a Windows 2012R2 server.
I really don’t know what’s going on here… I could certainly use win_shell module instead, but I would rather understand.
Any clue ?
Thank you.
Sure, the thing to understand is that win_command is for running executables directly. This means two things, 1/ the user’s environment isn’t applied and 2/ you aren’t running inside a dos box or powershell window - you are just starting a .exe (and receiving back whatever it sends to stdout and stderr).
‘dir’ isn’t actually an executable - its a built-in feature of the cmd.exe program. So to run dir via win_command, you’d have to run the cmd.exe program and pass it an argument to tell it do what ‘dir’ does, like this:
ansible windowshosts -m win_command -a 'cmd.exe /c dir'
Just to prove the point slightly, powershell also has ‘dir’ built-in to it. If you run
ansible windowshosts -m win_command -a 'powershell.exe /c dir'
You’ll get the dir output (but notice it is in a slightly different format from the cmd.exe version).
This may all seem odd as its unfamiliar to think of running windows commands without being inside either the cmd or powershell ‘shells’. But it is more secure to go straight to the .exe you want to use and run it without going via a shell, so it has its uses.
Jon