Ansible is known for its versatility in managing Linux systems, but can it manage Windows hosts? The answer is yes. Ansible provides robust support for automating Windows systems using Windows Remote Management (WinRM) or SSH. This article explains how to manage Windows hosts with Ansible, its prerequisites, and common use cases.
Can Ansible Manage Windows Hosts?
Yes, Ansible can effectively manage Windows hosts. By leveraging WinRM, Ansible communicates with Windows systems to perform configuration management, software deployment, and other administrative tasks.
Key Features:
- Agentless Architecture: Ansible uses WinRM or SSH to manage Windows systems without requiring additional software.
- Rich Module Support: Ansible provides Windows-specific modules for tasks like service management, file operations, and user management.
- Seamless Integration: Manage both Linux and Windows hosts from a single control node.
Prerequisites for Managing Windows Hosts with Ansible
1. Enable WinRM on Windows Hosts
WinRM is the primary communication protocol for managing Windows hosts with Ansible. To enable it:
- Open PowerShell as Administrator.
- Run the following commands:
winrm quickconfig winrm set winrm/config/service/auth '@{Basic="true"}' winrm set winrm/config/service '@{AllowUnencrypted="true"}' Set-Item wsman:\localhost\Client\TrustedHosts -Value "<Ansible_Control_Node_IP>"
2. Install pywinrm on the Control Node
The pywinrm Python library is required for Ansible to communicate with Windows hosts:
pip install pywinrm
3. Configure the Inventory File
Add the Windows host to your inventory file:
[windows]
windows_host ansible_host=192.168.1.10 ansible_user=Administrator ansible_password=your_password ansible_connection=winrm
Common Ansible Modules for Windows
Ansible offers a wide range of modules specifically designed for managing Windows systems:
1. win_service:
Manage Windows services.
- name: Ensure IIS is running
win_service:
name: W3SVC
state: started
2. win_package:
Install or uninstall software.
- name: Install Google Chrome
win_package:
path: "https://dl.google.com/chrome/install/GoogleChromeStandaloneEnterprise.msi"
3. win_user:
Create or manage user accounts.
- name: Add a new user
win_user:
name: dev_user
password: StrongPassword123!
state: present
4. win_file:
Manage files and directories.
- name: Create a directory
win_file:
path: C:\Temp
state: directory
5. win_shell:
Execute PowerShell or command-line commands.
- name: Run a PowerShell command
win_shell: Get-Service
Example Playbook for Managing Windows Hosts
Here’s a simple playbook to install and configure IIS on a Windows host:
- name: Configure Windows IIS
hosts: windows
tasks:
- name: Install IIS
win_feature:
name: Web-Server
state: present
- name: Ensure IIS service is running
win_service:
name: W3SVC
state: started
Use Cases for Managing Windows Hosts with Ansible
Software Deployment: Automate the installation and configuration of applications.
System Configuration: Apply consistent settings across multiple Windows systems.
Service Management: Start, stop, or restart services programmatically.
File and Directory Management: Create, modify, or delete files and directories.
User and Group Management: Add, remove, or update user accounts and groups.
Best Practices for Managing Windows Hosts
Secure Credentials: Use Ansible Vault to encrypt sensitive information like passwords.
Test Playbooks: Validate configurations in a staging environment before applying them to production.
Leverage Collections: Use Windows-specific collections like
ansible.windows
to simplify playbook development.Optimize Performance: Limit the scope of tasks to avoid unnecessary resource usage.
Conclusion
Ansible’s support for Windows hosts ensures seamless automation across heterogeneous environments. By following the prerequisites and leveraging Ansible’s rich module library, you can effectively manage and automate tasks on Windows systems.
Learn More About Managing Windows with Ansible
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.Academy
Discover more examples of Windows automation in Ansible by Examples.
Donate
Support this project by contributing today.