Setting up a Windows Server for Ansible Management
Tags: ansible, automation, windowsserver, setup
Published: 2023-10-08 14:21:31

---
title: Setting up a Windows Server for Ansible Management
published: true
date: 2023-09-24 10:31:53 UTC
tags: #ansible #automation #windowsserver #setup
canonical_url: https://anujdube.com/setting-up-a-windows-server-for-ansible-management
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cs2rpz9dr66wy6e3pd5k.png
---

Ansible, a widely used configuration management tool, is not just for Unix-like systems. With the right setup, it can also be used to manage Windows servers. This article will guide you through the process of setting up a Windows server to be controlled using Ansible.

For this demonstration, we'll be setting up two instances of "Microsoft Windows Server 2022 Base" as child nodes. Additionally, we'll create an Ubuntu instance to serve as our control node.

Ansible Uses SSH for connecting to Linux systems, And for Windows systems it uses Winrm.

## **Steps to Configure a Windows Server**

Open Powershell in Administrator mode and run the below command. it will run a script that will do all the necessary configurations. yes, that's it on the Window side.

```
Invoke-WebRequest -Uri https://raw.githubusercontent.com/rallabandisrinivas/winrm_ansible/main/README.md -UseBasicParsing | Select-Object -ExpandProperty Content | Invoke-Expression

```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695548596995/d2d5bf96-e994-4ecd-ba74-9f0cd0fa5afc.png)

You can refer to the official documentation below if you face any issues

[Ansible Windows Setup Link](https://docs.ansible.com/ansible/latest/os_guide/windows_setup.html)

## **Steps to Configure a Ubuntu Server**

**Step 1: Update Package list and upgrade Packages**
```
sudo apt update
sudo apt upgrade
```
Sometimes you may be asked to reboot then you can reboot using the below command.
```
sudo reboot
```

**Step 2: Install python3-pip and then install pywinrm package.**
```
sudo apt install python3-pip
pip install pywinrm
```

**Step 3: Let's install ansible**
```
sudo apt install ansible
```
**Step 4: Now create an inventory file and add IP and authentication info about Windows servers in it.**
*inventory*
```
 [windows]
 server1 ansible_host=65.2.122.214 ansible_user=Administrator ansible_password=3zA)--5TI$4pFng6*=qnAVLudgYtTqRP
 server2 ansible_host=15.206.194.23 ansible_user=Administrator ansible_password=3zA)--5TI$4pFng6*=qnAVLudgYtTqRP

 [windows:vars]
 ansible_connection=winrm
 ansible_winrm_server_cert_validation=ignore
```
Yes, Storing Passwords in plain text is a security risk we can use ansible-vault. But for the sake of the demo, we are storing it in plain text.

In Production environments, We can Use Ansible Vault to securely store and use passwords.

**Step 5: Now We will be running the below Adhoc command to test the connectivity**
```
ansible windows -m win_ping -i inventory
```
if you have created instances on the cloud and have not opened Winrm ports you might get the below error.
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695547185222/f3b37944-f565-4bdf-bf85-64473ad786d8.png)
Open those ports and run the command again
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695547246780/56c0be98-57b0-4fa4-93ff-373eaae3fc36.png)
Successful execution will look like the below
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695549971121/1acd5798-6ae6-4252-a8a7-a15d34b744bb.png)

----
Now, with all the Configurations in place, you can fully automate a myriad of tasks on your Windows servers.

Happy automating! 🤖