The win_shell module actually executes a command in the PowerShell where dir is an alias for Get-ChildItem. Get-ChildItem (and other PS cmdlets) do not have parameters or switches in the form of /* which is why you are getting this error. You can either change your task to use the PowerShell cmdlet format or change the shell to cmd. You can also use win_find if you want to have more advanced filters or search recursively so have a look at that if you want to filter on the files.
name: change shell to cmd
win_shell: dir /b c:\temp
args:
executable: cmd
`
Here is the output of both of those tasks
In your case I would go with the cmd option as it is faster (5s as opening Powershell takes time) and less verbose but the PS option is good if you need to save variables or do something with the data before sending it back to Ansible.
Thanks a million. I was able to get the output I’m looking for. Now that have the list of files in my C:\Temp, how do I fetch those files to my ansible controller host? Here’s an example I have so far but failing at one section.
TASK [get the files] ****************************************************************************************************************************************************************************************************************************************************************************************************
fatal: [SV1]: FAILED! => {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘item’ is undefined\n\nThe error appears to have been in ‘/home/vagrant/ansible/find-copy-test.yml’: line 12, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: get the files\n ^ here\n”}
fatal: [SV2]: FAILED! => {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘item’ is undefined\n\nThe error appears to have been in ‘/home/vagrant/ansible/find-copy-test.yml’: line 12, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: get the files\n ^ here\n”}
to retry, use: --limit @/home/vagrant/ansible/find-copy-test.retry
Your indent for with_items is incorrect, it should be set on the task level and not module level, so
`
name: get the files
fetch:
src: “C:/Temp/{{ item }}”
dest: /home/vagrant/win-script-result/
flat: yes
with_items: “{{ results.stdout_lines }}”
`
I would also highly recommend swapping over to the win_find module instead of running that command. It gives you more control over what to find as you can add multiple regex strings to search and gives you room to expand in the future. This is what it would look like;
Both paths and patterns can take in a list so you can specify multiple paths and patterns instead of running separate tasks. It also gives you the ability to use regex if you really feel like hurting yourself
The solutions you’ve provided worked out fine. I ended up using win_shell module simply because I was not able to pass %computername% variable in win_find patterns. I’m a beginner and ready to tackle regex
I really appreciate your help. Also, by any chance the values in results.stdout_lines can be used in combication with the archive module to tar the files in result?
or if you need to use the hostname as defined in your inventory
paths: "C:\\Temp\\*{{inventory_hostname}}*"
One of the nice things about win_find is it will give you a better structured set of results than using win_shell and looping through the results.stdout_lines
fetch is intended to be tolerant of files being missing, so it won’t choke if you pass it the results.stdout_lines as items.
However as you want to use archive to tar up the files you have fetched, win_find should probably give you a better quality list.
You can use archive (assuming it does what you want) by delegating to localhost, like this (untested)