Manage Windows Machines with Ansible

In this blog post, I will show you how to manage Windows Server or Client with Ansible.

For many years Ansible was only capable of managing Linux machine; however, things have changed recently.

For Windows machine management Ansible is using WinRM and not SSH.

Enable WinRM listener

To enable WinRM on my Windows machine, I will allow WinRM (if it is not enabled) using the following command.

winrm quickconfig

Note: Make sure you run it as Administrator.

Run Script and Enable

Ansible has a GitHub script you can run on your machine that will configure WinRM with many recommended settings.

I recommend you use this method:

https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

Check Listener

After enabling WinRM, you can check that it is enabled using the following command.

winrm enumerate winrm/config/Listener

Note: You should see the output below.

Install WinRM on Control Host

To manage, Windows machine, we need to install the WinRM deployment package for Linux.

You install it using the following commands.

yum -y install python-pip
pip install "pywinrm>=0.2.2"
pip install "pywinrm>=0.3.0"

Note: I am installing two versions because version 0.3 might not work if you have Python 2 on your Ansible node.

Hosts File – Windows

In the hosts’ file (/etc/ansible/hosts) create a Windows collection and add your machine.

You will also need to add the var section below it and enter the username and password of your Windows user to login to the machines.

[windows]
172.16.0.5
 
[windows:vars]
ansible_user=administrator
ansible_password=enter_server_password
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

Test

To test that everything is working I will run the following command that will ping my host.

ansible windows -i hosts -m win_ping

Processing…
Success! You're on the list.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.