"Win_Command" module not working to connect localhost to run Powershell script from ansible which is installed in windows10

47 views
Skip to first unread message

Pavan K Kumar

unread,
Apr 16, 2020, 4:59:37 AM4/16/20
to Ansible Project

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/


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! 

Jordan Borean

unread,
Apr 16, 2020, 6:43:08 AM4/16/20
to Ansible Project
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.

Pavan K Kumar

unread,
Apr 16, 2020, 7:14:41 AM4/16/20
to Ansible Project
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!!

Thanh Nguyen Duc

unread,
Apr 16, 2020, 9:19:05 AM4/16/20
to ansible...@googlegroups.com
Localhost is your ansible server and it doesn’t work with windown module, you need to specify the client group name

Sent from my iPhone

On Apr 16, 2020, at 16:59, Pavan K Kumar <pavank...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0a657327-034b-4131-a9b7-9ee99364889a%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages