Ansible Windows Modules
Introduction
Ansible is a powerful open-source automation tool that can manage configurations, deploy applications, and orchestrate more complex IT tasks. While Ansible was originally designed for Linux/Unix systems, it has robust support for Windows automation through specialized Windows modules.
Windows modules in Ansible allow you to manage Windows servers just as effectively as Linux servers, all from a Linux control machine. These modules handle Windows-specific operations such as managing Windows features, services, registry settings, and more.
Prerequisites
Before working with Ansible Windows modules, you need:
- An Ansible control node (Linux/Unix)
- Windows target hosts with:
- PowerShell 3.0 or later
- .NET Framework 4.0 or later
- WinRM service configured for remote management
Setting Up WinRM
Windows Remote Management (WinRM) must be configured on Windows hosts. Here's a quick setup script you can run in PowerShell with administrator privileges:
$url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:temp\ConfigureRemotingForAnsible.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy Bypass -File $file
Configuring Ansible for Windows
Your Ansible inventory file should specify Windows hosts with connection variables:
[windows]
windows-server1.example.com
windows-server2.example.com
[windows:vars]
ansible_user=Administrator
ansible_password=SecurePassword
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore
Core Windows Modules
win_feature - Managing Windows Features
The win_feature
module installs or uninstalls Windows features.
Example: Installing IIS
- name: Install IIS Web Server
win_feature:
name: Web-Server
state: present
include_management_tools: yes
register: iis_install
- name: Reboot if required
win_reboot:
when: iis_install.reboot_required
Output:
TASK [Install IIS Web Server] ************************************
ok: [windows-server1.example.com]
TASK [Reboot if required] ***************************************
skipping: [windows-server1.example.com]
win_service - Managing Windows Services
Control Windows services with the win_service
module.
Example: Configuring a Service
- name: Configure Windows Update service
win_service:
name: wuauserv
start_mode: manual
state: stopped
Output:
TASK [Configure Windows Update service] **************************
changed: [windows-server1.example.com]
win_package - Installing Software
Install MSI or EXE packages on Windows hosts.
Example: Installing 7-Zip
- name: Install 7-Zip
win_package:
path: https://www.7-zip.org/a/7z1900-x64.msi
state: present
product_id: '{23170F69-40C1-2702-1900-000001000000}'
Registry Management
win_regedit - Managing Registry Keys
Manage Windows registry settings.
Example: Setting Registry Values
- name: Configure RDP port
win_regedit:
path: HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp
name: PortNumber
data: 3389
type: dword
File Management
win_file - Managing Files and Directories
Create, delete, or set attributes on files and directories.
Example: Creating a Directory
- name: Create application directory
win_file:
path: C:\App\Logs
state: directory
win_copy - Copying Files
Copy files to Windows hosts.
Example: Deploying a Configuration File
- name: Copy configuration file
win_copy:
src: ./files/app-config.xml
dest: C:\App\config.xml
Working with ACLs
win_acl - Managing File Permissions
Manage file and directory permissions.
Example: Setting Permissions
- name: Set permissions on a file
win_acl:
path: C:\App\sensitive-data.txt
user: domain\app_service
rights: ReadAndExecute
type: allow
state: present