# "Win\_Command" module not working to connect localhost to run Powershell script from ansible which is installed in windows10

**URL:** <https://forum.ansible.com/t/win-command-module-not-working-to-connect-localhost-to-run-powershell-script-from-ansible-which-is-installed-in-windows10/32695>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [April 16, 2020, 8:59am UTC](https://forum.ansible.com/t/win-command-module-not-working-to-connect-localhost-to-run-powershell-script-from-ansible-which-is-installed-in-windows10/32695 "2020-04-16T08:59:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pavan\_K\_Kumar](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Pavan\_K\_Kumar](https://forum.ansible.com/u/Pavan_K_Kumar)\
**Post date:** [April 16, 2020, 8:59am UTC](https://forum.ansible.com/t/win-command-module-not-working-to-connect-localhost-to-run-powershell-script-from-ansible-which-is-installed-in-windows10/32695/1 "2020-04-16T08:59:37Z")

</div>

Hello Experts,

I’m new to Ansible and PowerShell.

I have installed cygwin in my windows 10 Operating system and installed ansible and its related packages as per below mail.

[https://geekflare.com/ansible-installation-windows/](https://geekflare.com/ansible-installation-windows/)

**Here are the environment details**

Ansible version: 2.8.4  
python version : 3.7.7 (default, Apr 10 2020, 07:59:19) [GCC 9.3.0]

Powershell Version: 5.1.17763.1007

on above successful setup, i tried to run my below sample ansible script with hosts as “localhost”

> - hosts: localhost
> 
> tasks:
> 
> - name: “run whoami”
> 
> win\_command: whoami
> 
> register: whoami\_out
> 
> - debug: var=whoami\_out.stdout\_lines

but, i am ending up with the below error

> 1. [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match ‘all’
> 
> PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [localhost]
> 
> TASK [run whoami] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [localhost]: FAILED! =\> {“changed”: false, “module\_stderr”: “/home/Pavan.Kammara/.ansible/tmp/ansible-tmp-1586957425.671443-215768864612631/AnsiballZ\_win\_command.ps1 : The term \r\n’/home/Pavan.Kammara/.ansible/tmp/ansible-tmp-1586957425.671443-215768864612631/AnsiballZ\_win\_command.ps1’ is not \r\nrecognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if \r\na path was included, verify that the path is correct and try again.\r\nAt line:1 char:1\r\n+ /home/Pavan.Kammara/.ansible/tmp/ansible-tmp-1586957425.671443-215768 …\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \r\n + CategoryInfo : ObjectNotFound: (/home/Pavan.Kam…win\_command.ps1:String) , CommandNotFoundException\r\n + FullyQualifiedErrorId : CommandNotFoundException\r\n \r\n”, “module\_stdout”: “”, “msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”, “rc”: 1}
> 
> PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

and i tried the 2nd approach by creating my windows 10 machine ip address in etc/ansible/ansible.cfg and referred the same in asnible playbook as below

> - hosts: all  
> tasks:
> - name: “run whoami”  
> win\_command: whoami  
> register: whoami\_out
> - debug: var=whoami\_out.stdout\_lines

this time also ended up with the different error, PFB

> PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [192. **_._.** _]: UNREACHABLE! =\> {“changed”: false, “msg”: "Failed to connect to the host via ssh: ssh: connect to host 192.__.._\*\* port 22: Connection refused", “unreachable”: true}
> 
> PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> 192. **_._.** \* : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignore

Kindly help me, is it something the environment set up issue. Im completely clueless why it is not working as im new to this technology.

Thanks in advance!

---

<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:** [April 16, 2020, 10:43am UTC](https://forum.ansible.com/t/win-command-module-not-working-to-connect-localhost-to-run-powershell-script-from-ansible-which-is-installed-in-windows10/32695/2 "2020-04-16T10:43:08Z")

</div>

You cannot run Windows modules over localhost, they are designed to run through a connection plugin like winrm, psrp, or ssh. Also I would highly advise not running Ansible through cygwin, it may work in some places but will then fall flat in others. If you are on Windows 10 then WSL is a lot better in terms of compatibility but nothing will trump a VM running Linux. You also avoid issue like this where you try to run Windows stuff on localhost when Ansible isn’t designed for that.

---

<div class="post-metadata">

**Author:** ![Pavan\_K\_Kumar](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Pavan\_K\_Kumar](https://forum.ansible.com/u/Pavan_K_Kumar)\
**Post date:** [April 16, 2020, 11:14am UTC](https://forum.ansible.com/t/win-command-module-not-working-to-connect-localhost-to-run-powershell-script-from-ansible-which-is-installed-in-windows10/32695/3 "2020-04-16T11:14:40Z")

</div>

Hello Jordan,

Thank you very much for your reply. that is very much informative.

could you please guide how do i can setup the basic environment to play around ansible with powershell.

i need to develop a scripts, which needs to communicate each other **(Powershell script ↔ Ansible)** to achieve some functionality.

Thank you again!!

---

<div class="post-metadata">

**Author:** ![Thanh\_Nguyen\_Duc](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@Thanh\_Nguyen\_Duc](https://forum.ansible.com/u/Thanh_Nguyen_Duc)\
**Post date:** [April 16, 2020, 1:19pm UTC](https://forum.ansible.com/t/win-command-module-not-working-to-connect-localhost-to-run-powershell-script-from-ansible-which-is-installed-in-windows10/32695/4 "2020-04-16T13:19:10Z")

</div>

Localhost is your ansible server and it doesn’t work with windown module, you need to specify the client group name
