Table of Contents


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

ComponentRequirement
Control NodeLinux or macOS with Python 3.8+
Target HostWindows Server 2016 / 2019 / 2022
NetworkPort 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 pywinrm

Verify the installation:

ansible --version

Step 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/listener

Production 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=ignore

Security 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_ping

Expected 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.yml

Sample 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

Store credentials securely instead of in plain text:

# Create an encrypted vars file
ansible-vault create group_vars/windows/vault.yml

Inside the vault file:

vault_ansible_password: YourSecurePassword123

Reference 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-pass

Common Windows Ansible Modules

ModulePurpose
win_pingTest connectivity
win_featureInstall/remove Windows features (IIS, etc.)
win_serviceManage Windows services
win_fileCreate/delete files and directories
win_copyCopy files to Windows hosts
win_shellRun PowerShell commands
win_packageInstall/uninstall software (.exe/.msi)
win_userManage local Windows users
win_rebootReboot and wait for host to come back
win_updatesInstall 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=ignore for 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