How to run Ansible Windows Module

by Anish

Posted on Tuesday June 25, 2019


Ansible Windows module uses winrm connection, in order to execute commands in the Windows machine. There are many module supported by the ansible

Let's setup a BASIC ping-pong using win_ping module

This is the directory struture we are going to build


|--group_vars
|------all.yml
|--hosts
|--test.yml

The all.yml file in the group_vars directory will hold necessary credentials for Windows Authentication. When setting up the inventory, the following variables are required:

---
ansible_ssh_user: "Administrator"
ansible_ssh_pass: "Administrator"
ansible_ssh_port: "5985"
ansible_connection: "winrm"
ansible_winrm_server_cert_validation: ignore
  • By default WinRM : The default HTTP port is 5985.
  • By default, basic Authentication or if kerberos module is installed it will use kerberos.

The test.yml is the ansible-playbook which is using win_ping module to. A windows version of the classic ping module

---
- hosts: win
  tasks:
   - name: Ping Windows Hosts
     win_ping:

Run the Playbook

$ ansible-playbook  test.yml  -i hosts
PLAY [win] **********************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [10.65.148.49]
TASK [Ping Windows Hosts] *******************************************************************************************************************************************
ok: [10.65.148.49]
PLAY RECAP **********************************************************************************************************************************************************
10.65.148.49 : ok=2 changed=0  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0

We tested sucessfully ping command, now jump to another ansible windows module win_shell which will help to execute any command in windows from the controller node.

Here is the sample ansible-playbook for win_shell.yml

---
- hosts: win
  tasks:
  - name: Execute command in Windows
    win_shell: dir
    args:
        executable: cmd
    register: out

  - debug: var=out.stdout_lines

Run the playbook.

$ ansible-playbook  win_shell.yml  -i hosts 

PLAY [win] **********************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [10.65.148.49]

TASK [Execute command in Windows] ***********************************************************************************************************************************
changed: [10.65.148.49]

TASK [debug] ********************************************************************************************************************************************************
ok: [10.65.148.49] => {
    "out.stdout_lines": [
        " Volume in drive C has no label.", 
        " Volume Serial Number is D0D0-E635", 
        "", 
        " Directory of C:\\Users\\Administrator", 
        "", 
        "06/24/2019  04:03 AM    <DIR>          .", 
        "06/24/2019  04:03 AM    <DIR>          ..", 
        "06/17/2019  08:45 PM    <DIR>          Contacts", 
        "06/17/2019  08:45 PM    <DIR>          Desktop", 
        "06/17/2019  08:45 PM    <DIR>          Documents", 
        "06/17/2019  08:45 PM    <DIR>          Downloads", 
        "06/17/2019  08:45 PM    <DIR>          Favorites", 
        "06/17/2019  08:45 PM    <DIR>          Links", 
        "06/17/2019  08:45 PM    <DIR>          Music", 
        "06/17/2019  08:45 PM    <DIR>          Pictures", 
        "06/17/2019  08:45 PM    <DIR>          Saved Games", 
        "06/17/2019  08:45 PM    <DIR>          Searches", 
        "06/17/2019  08:45 PM    <DIR>          Videos", 
        "               0 File(s)              0 bytes", 
        "              13 Dir(s)  64,714,444,800 bytes free"
    ]
}
PLAY RECAP **********************************************************************************************************************************************************
10.65.148.49               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Video Demo


Thanku for reading !!! Give a Share for Support


Your Support Matters!

Instead of directly asking for donations, I'm thrilled to offer you all nine of my books for just $9 on leanpub By grabbing this bundle you not only help cover my coffee, beer, and Amazon bills but also play a crucial role in advancing and refining this project. Your contribution is indispensable, and I'm genuinely grateful for your involvement in this journey!

Any private key value that you enter or we generate is not stored on this site, this tool is provided via an HTTPS URL to ensure that private keys cannot be stolen, for extra security run this software on your network, no cloud dependency




python Cryptography Topics
Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Kubernetes for DevOps

Hello Dockerfile

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers

Here