Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ansible getting error running a Windows "dir" command?

$ ansible --version
ansible 2.10.8

I have a simple task to list the *.zip file in a Windows folder

- name: Get name of old file
  win_command: dir /b *.zip
  args:
    chdir C:\\temp

and I get

TASK [list-files : Get name of old file] ******************************************
fatal: [10.x.x.x]: FAILED! => {"changed": false, "cmd": "dir /b *.zip", "msg": "Exception calling \"SearchPath\" with \"1\" argument(s): \"Could not find file 'dir.exe'.\"", "rc": 2}

However, this works

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ansible -i 10.x.x.x, 10.x.x.x -m win_command -a "cmd.exe /c dir c:\temp\*.zip"

Any clues?

>Solution :

FAIR WARNING: I don’t have a copy of Windows to test this, but still hope it’s helpful…

Since dir is (as the error helpfully explains) not an executable but rather a shell built-in, you’ll want to use win_shell: instead of the command version

As the fine manual points out, the default shell is PowerShell, so ensure you provide the executable: cmd to put it back to the old one if you really want to use that DOS style invocation versus Get-ChildItem -Recursive

- name: Get name of old file
  win_shell: dir /b *.zip
  args:
    executable: cmd
    chdir: C:\\temp
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading