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:
1. Open PowerShell as Administrator.
2. Run the following commands:
``powershell
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:
`bash
pip install pywinrm
`
3. Configure the Inventory File
Add the Windows host to your inventory file:
`ini
[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.
`yaml
- name: Ensure IIS is running
win_service:
name: W3SVC
state: started
`
2. win_package:
Install or uninstall software.
`yaml
- name: Install Google Chrome
win_package:
path: "https://dl.google.com/chrome/install/GoogleChromeStandaloneEnterprise.msi"
`
3. win_user:
Create or manage user accounts.
`yaml
- name: Add a new user
win_user:
name: dev_user
password: StrongPassword123!
state: present
`
4. win_file:
Manage files and directories.
`yaml
- name: Create a directory
win_file:
path: C:\Temp
state: directory
`
5. win_shell:
Execute PowerShell or command-line commands.
`yaml
- 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