win_command problems

Hi,
so if I run on the command line, having used the inventory file to add user/password details under windows:vars
ansible mesh -u user1 -m win_shell -a “dir chdir=C:\Temp”

then I get

mesh | SUCCESS | rc=0 >>

Directory: C:\Temp

Mode LastWriteTime Length Name

But with a playbook

  • name: Run a series of debug tasks to see the value of variables
    hosts: mesh
    user: user1
    tasks:
  • name: list all the files in temp on c drive
    win_command: dir
    args:
    chdir: c:\temp

I get
TASK [list all the files in temp on c drive] ***********************************
fatal: [mesh]: FAILED! => {“changed”: false, “cmd”: “dir”, “failed”: true, “msg”: “The system cannot find the file specified”, “rc”: 2}

The win_command module docs show do not show escaping of backslashes or quotes.

What am I doing wrong?

Regards,
John

You’re using the wrong module. ‘dir’ isn’t a command, it’s a shell directive- you need to give that to Powershell or cmd.exe, so you need to use win_shell. win_command is for running an executable.

-Matt

Thanks for this Matt. Yes that makes a lot of sense.