# win\_shell - What's the proper way to use a command with a flag in ansible playbook or adhoc command.

**URL:** <https://forum.ansible.com/t/win-shell-whats-the-proper-way-to-use-a-command-with-a-flag-in-ansible-playbook-or-adhoc-command/25722>\
**Category:** Ansible Project\
**Created:** [October 11, 2017, 8:32pm UTC](https://forum.ansible.com/t/win-shell-whats-the-proper-way-to-use-a-command-with-a-flag-in-ansible-playbook-or-adhoc-command/25722 "2017-10-11T20:32:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcalfa1](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@marcalfa1](https://forum.ansible.com/u/marcalfa1)\
**Post date:** [October 11, 2017, 8:32pm UTC](https://forum.ansible.com/t/win-shell-whats-the-proper-way-to-use-a-command-with-a-flag-in-ansible-playbook-or-adhoc-command/25722/1 "2017-10-11T20:32:05Z")

</div>

Trying to “dir /b c:\Temp” using a playbook or via adhoc and getting the error message below. It seems to have a problem with “/b” in “Dir /b C:\Temp”

**- hosts: all**  
**gather\_facts: yes**  
**tasks:**

**- win\_shell: dir /b C:\Temp**

FAILED! =\> {“changed”: true, “cmd”: “dir /b C:\Temp”, “delta”: “0:00:00.839042”, “end”: “2017-10-11 08:22:02.820065”, “failed”: true, “rc”: 1, “start”: “2017-10-11 08:22:01.981022”, “stderr”: “dir : Cannot find path ‘C:\b’ because it does not exist.\r\nAt line:1 char:65\r\n+ [Console]::InputEncoding = New-Object Text.UTF8Encoding $false; dir /b \r\nC:\Temp\r\n+ \r\n~~~~~~~~~~~~~~\r\n + CategoryInfo : ObjectNotFound: (C:\b:String) [Get-ChildItem], I \r\n temNotFoundException\r\n + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCh \r\n ildItemCommand”, “stderr\_lines”: [“dir : Cannot find path ‘C:\b’ because it does not exist.”, “At line:1 char:65”, "+ [Console]::InputEncoding = New-Object Text.UTF8Encoding $false; dir /b ", “C:\Temp”, "+ ", “~~~~~~~~~~~~~~”, " + CategoryInfo : ObjectNotFound: (C:\b:String) [Get-ChildItem], I “, " temNotFoundException”, " + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCh “, " ildItemCommand”], “stdout”: “”, “stdout\_lines”: }

**ansible -i poc-win all -m raw -a “dir /b C:\Temp”**

> FAILED | rc=1 \>\>  
> **dir : Cannot find path ‘C:\b’ because it does not exist**.  
> At line:1 char:1

- dir /b C:\Temp
- 

```auto

```

- CategoryInfo : ObjectNotFound: (C:\b:String) [Get-ChildItem], I  
temNotFoundException
- FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCh  
ildItemCommand

**ansible -i poc-win all -m win\_shell -a “dir /b C:\Temp”**

> FAILED | rc=1 \>\>  
> dir : Cannot find path ‘C:\b’ because it does not exist.  
> At line:1 char:65

- [Console]::InputEncoding = New-Object Text.UTF8Encoding $false; dir /b  
C:\Temp

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [October 11, 2017, 9:02pm UTC](https://forum.ansible.com/t/win-shell-whats-the-proper-way-to-use-a-command-with-a-flag-in-ansible-playbook-or-adhoc-command/25722/2 "2017-10-11T21:02:29Z")

</div>

Hi

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: using native PS cmdlet  
win\_shell: Get-ChildItem -Path C:\Temp | ForEach-Object { $\_.Name }

- name: change shell to cmd  
win\_shell: dir /b c:\temp  
args:  
executable: cmd  
`

Here is the output of both of those tasks

[![](https://lh3.googleusercontent.com/-kmie9HG5SXk/Wd6GOD6cjnI/AAAAAAAAAQg/n_FT9OolfJQLpt_elaJmoPbHT7vyq1q4gCLcBGAs/s1600/Screen%2BShot%2B2017-10-12%2Bat%2B6.59.03%2Bam.png)](https://lh3.googleusercontent.com/-kmie9HG5SXk/Wd6GOD6cjnI/AAAAAAAAAQg/n_FT9OolfJQLpt_elaJmoPbHT7vyq1q4gCLcBGAs/s1600/Screen%2BShot%2B2017-10-12%2Bat%2B6.59.03%2Bam.png)

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

Jordan

---

<div class="post-metadata">

**Author:** ![marcalfa1](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@marcalfa1](https://forum.ansible.com/u/marcalfa1)\
**Post date:** [October 12, 2017, 12:04am UTC](https://forum.ansible.com/t/win-shell-whats-the-proper-way-to-use-a-command-with-a-flag-in-ansible-playbook-or-adhoc-command/25722/3 "2017-10-12T00:04:32Z")

</div>

Jordan,

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.

**- hosts: all**  
**gather\_facts: yes**  
**tasks:**

**- win\_shell: dir /b C:\Temp\*.tar**  
**args:**  
**executable: cmd**  
**register: results**  
**- debug:**  
**var: results.stdout\_lines**

**- name: get the files**  
**fetch:**  
**src: “C:/Temp/{{ item }}”**  
**dest: /home/vagrant/win-script-result/**  
**flat: yes**  
**with\_items: “{{ results.stdout\_lines }}”**

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\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\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [SV1] =\> {  
“results.stdout\_lines”: [  
“1709051418.tar”,  
“1709051525.tar”,  
“1709052115.tar”  
]  
}  
ok: [SV2] =\> {  
“results.stdout\_lines”: [  
“1709051421.tar”,  
“1709051527.tar”,  
“1709052107.tar”  
]  
}

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

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [October 12, 2017, 1:13am UTC](https://forum.ansible.com/t/win-shell-whats-the-proper-way-to-use-a-command-with-a-flag-in-ansible-playbook-or-adhoc-command/25722/4 "2017-10-12T01:13:27Z")

</div>

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;

`

- name: find tar files  
win\_find:  
paths: C:\temp  
file\_type: file  
patterns: ‘\*.tar’  
register: tar\_files

- name: fetch tar files  
fetch:  
src: ‘{{ item.path }}’  
dest: /home/vagrant/win-script-result/  
flat: yes  
with\_items: ‘{{ tar\_files.files }}’  
`

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 🙂

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![marcalfa1](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@marcalfa1](https://forum.ansible.com/u/marcalfa1)\
**Post date:** [October 13, 2017, 2:37am UTC](https://forum.ansible.com/t/win-shell-whats-the-proper-way-to-use-a-command-with-a-flag-in-ansible-playbook-or-adhoc-command/25722/5 "2017-10-13T02:37:31Z")

</div>

Jordan,

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?

Thanks in advance.

**- hosts: all**  
**gather\_facts: yes**  
**tasks:**

**- win\_shell: dir /b C:\Temp\*%computername%\***  
**args:**  
**executable: cmd**  
**register: results**  
**- debug:**  
**var: results.stdout\_lines**

**- name: get the files**  
**fetch:**  
**src: “C:/Temp/{{ item }}”**  
**dest: /home/vagrant/win-script-result/**  
**flat: yes**  
**with\_items: “{{ results.stdout\_lines }}”**

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [October 13, 2017, 7:46am UTC](https://forum.ansible.com/t/win-shell-whats-the-proper-way-to-use-a-command-with-a-flag-in-ansible-playbook-or-adhoc-command/25722/6 "2017-10-13T07:46:28Z")

</div>

For win\_find, you can probably use a paths like

`paths: "C:\\Temp\\*{{ansible_hostname}}*"`

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)

`

- name: archive fetched files  
archive:  
path: /home/vagrant/win-script-result/  
dest: /home/vagrant/results.tgz  
delegate\_to: localhost

`

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [October 13, 2017, 7:49am UTC](https://forum.ansible.com/t/win-shell-whats-the-proper-way-to-use-a-command-with-a-flag-in-ansible-playbook-or-adhoc-command/25722/7 "2017-10-13T07:49:24Z")

</div>

Oh, meant to say ansible\_hostname and inventory\_hostname are documented here: [http://docs.ansible.com/ansible/latest/playbooks\_variables.html](http://docs.ansible.com/ansible/latest/playbooks_variables.html)
