Connecting with windows machine using ansible

How to connect with Windows machine using Ansible

Last Updated on February 18, 2023 by cscontents


Connecting with a Windows machine using Ansible is little different from its Linux counterpart. In this post we will discuss how we can connect with a windows host using Ansible so that we can execute task remotely.

Ansible connects with Windows machine using WinRM. For this we need to install pywinrm module of python which will used for WinRM connectivity. For more details please follow this article Introduction to Ansible | High Level Understanding of Ansible


Before going through this article I will assume you have knowledge about the below.

  • Hands-on knowledge on Ansible
  • Basics of networking

If you want to learn Ansible basics then please check out the below course from KodeKloud which offers one of the best learning material in DevOps world.

Link of Training Course: Ansible for absolute beginners

Note: The above links are affiliate links, if you enroll this course using the above link, then it would help us to get some monetary benefit from KodeKloud. It won’t cost you anything.

Step-by-Step guide

To connect with windows host using Ansible, we need to follow the below steps –

Step – 1

Ansible host or Ansible controller should be ready. It means on a Linux machine we need to install Ansible & use it as Ansible host.

ubuntu@ubuntu-1:~$ python3 --version
Python 3.8.10
ubuntu@ubuntu-1:~$ ansible --version
ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]

Step – 2

Install pywinrm module of python. For this we need pip.

To verify we can run below command.

ubuntu@ubuntu-1:~$ python3 -m pip -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

Using pip we can install pywinrm by running the below command (don’t run the command as root user).

python3.x -m pip install --user pywinrm
Note: Replace x by your python version

Step – 3

Ensure below is port open between ansible-host and target Windows machine.

  • 5985 port should be open if we want to use HTTP.
  • 5986 port should be open if we want to use HTTPS

To check the port we can use ‘telnet’.

telnet <IP of Windows server> 5985
telnet <IP of Windows server> 5986

Step – 4

In this step we will check whether “WinRM” service is running or not in the target Windows server. This “WinRM” service need to be running on the target Windows server.

We can check the “WinRM” config by running the below command in PowerShell.

winrm quickconfig

Step – 5

This is the final step, in this step we will test the connectivity. For this we will use “win_ping” module of ansible.

We need to create an inventory file which will contain details about the target Windows machine. So we will create an inventory.txt file and add the below content to it. Replace the IP and user, password with your details.


Below is the ansible command we need to run,

ubuntu@ubuntu-1:~/tmp-ping$ ansible remote_server -i inventory.txt -m win_ping
20.28.x.x | SUCCESS => {
"changed": false,
"ping": "pong"

After running the above command if you get output similar to above, then congratulations! You are able to connect with target Windows machine successfully.

Thank You.


If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.