Table of Contents
- Overview
- Prerequisites
- Step 1 — Install Ansible on the Control Node
- Step 2 — Enable WinRM on the Windows Server
- Step 3 — Create the Ansible Inventory File
- Step 4 — Test Connectivity
- Step 5 — Write a Playbook
- Step 6 — Run the Playbook
- Step 7 — Use Ansible Vault for Secrets (Recommended)
- Common Windows Ansible Modules
- Troubleshooting
- Project Structure (Summary)
Overview
Ansible communicates with Windows hosts using WinRM (Windows Remote Management) instead of SSH. This guide walks through setting up a control node (Linux/macOS) to manage a Windows Server target.
Prerequisites
| Component | Requirement |
|---|---|
| Control Node | Linux or macOS with Python 3.8+ |
| Target Host | Windows Server 2016 / 2019 / 2022 |
| Network | Port 5985 (HTTP) or 5986 (HTTPS) open |
Step 1 — Install Ansible on the Control Node
# Ubuntu / Debian
sudo apt update && sudo apt install -y ansible python3-pip
# Install the Windows support library
pip3 install pywinrmVerify the installation:
ansible --versionStep 2 — Enable WinRM on the Windows Server
Run the following commands in an elevated PowerShell session on the Windows target:
# Enable WinRM service
Enable-PSRemoting -Force
# Allow basic authentication (required for Ansible)
Set-Item -Path WSMan:\localhost\Service\Auth\Basic -Value $true
# Allow unencrypted traffic (for HTTP/lab use; use HTTPS in production)
Set-Item -Path WSMan:\localhost\Service\AllowUnencrypted -Value $true
# Open the WinRM firewall port
netsh advfirewall firewall add rule name="WinRM HTTP" protocol=TCP dir=in localport=5985 action=allow
# Confirm WinRM is listening
winrm enumerate winrm/config/listenerProduction tip: Use HTTPS (port 5986) with a proper certificate instead of plain HTTP.
Step 3 — Create the Ansible Inventory File
On your control node, create a file named inventory.ini:
[windows]
win-server-01 ansible_host=192.168.1.100
[windows:vars]
ansible_user=Administrator
ansible_password=YourSecurePassword123
ansible_connection=winrm
ansible_winrm_transport=basic
ansible_port=5985
ansible_winrm_server_cert_validation=ignoreSecurity note: In production, store credentials in Ansible Vault instead of plain text.
Step 4 — Test Connectivity
Ping the Windows host using the win_ping module:
ansible windows -i inventory.ini -m win_pingExpected output:
win-server-01 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Step 5 — Write a Playbook
Create a file named windows_setup.yml:
---
- name: Configure Windows Server
hosts: windows
gather_facts: yes
tasks:
- name: Gather OS information
ansible.builtin.debug:
msg: "Managing {{ ansible_hostname }} running {{ ansible_os_name }}"
- name: Ensure IIS feature is installed
ansible.windows.win_feature:
name: Web-Server
state: present
include_management_tools: yes
- name: Create a directory
ansible.windows.win_file:
path: C:\AppData\MyApp
state: directory
- name: Copy a configuration file
ansible.windows.win_copy:
src: files/app.config
dest: C:\AppData\MyApp\app.config
- name: Ensure the W3SVC (IIS) service is running
ansible.windows.win_service:
name: W3SVC
state: started
start_mode: auto
- name: Run a PowerShell command
ansible.windows.win_shell: |
Get-Service | Where-Object { $_.Status -eq 'Running' } | Measure-Object
register: running_services
- name: Show running service count
ansible.builtin.debug:
msg: "Running services: {{ running_services.stdout.trim() }}"Step 6 — Run the Playbook
ansible-playbook -i inventory.ini windows_setup.ymlSample output:
PLAY [Configure Windows Server] ****************************
TASK [Gather OS information] *******************************
ok: [win-server-01] => {
"msg": "Managing WIN-SERVER-01 running Windows Server 2022 Standard"
}
TASK [Ensure IIS feature is installed] *********************
changed: [win-server-01]
TASK [Create a directory] **********************************
changed: [win-server-01]
...
PLAY RECAP *************************************************
win-server-01 : ok=7 changed=3 unreachable=0 failed=0
Step 7 — Use Ansible Vault for Secrets (Recommended)
Store credentials securely instead of in plain text:
# Create an encrypted vars file
ansible-vault create group_vars/windows/vault.ymlInside the vault file:
vault_ansible_password: YourSecurePassword123Reference it in your inventory:
[windows:vars]
ansible_password={{ vault_ansible_password }}Run the playbook with the vault password:
ansible-playbook -i inventory.ini windows_setup.yml --ask-vault-passCommon Windows Ansible Modules
| Module | Purpose |
|---|---|
win_ping | Test connectivity |
win_feature | Install/remove Windows features (IIS, etc.) |
win_service | Manage Windows services |
win_file | Create/delete files and directories |
win_copy | Copy files to Windows hosts |
win_shell | Run PowerShell commands |
win_package | Install/uninstall software (.exe/.msi) |
win_user | Manage local Windows users |
win_reboot | Reboot and wait for host to come back |
win_updates | Install Windows Updates |
Troubleshooting
Connection refused on port 5985
- Confirm WinRM is running:
Get-Service WinRM - Check firewall rules allow port 5985
Authentication errors
- Ensure Basic auth is enabled on the WinRM listener
- Verify the username includes the domain if needed:
DOMAIN\Administrator
SSL certificate errors
- Set
ansible_winrm_server_cert_validation=ignorefor lab use - Use a valid certificate in production
Project Structure (Summary)
ansible-windows/
├── inventory.ini
├── windows_setup.yml
├── files/
│ └── app.config
└── group_vars/
└── windows/
└── vault.yml