Ansible is a powerful tool for automating tasks across various platforms, including Windows systems. While it’s widely known for managing Linux, Ansible’s support for Windows enables seamless cross-platform automation. This article explains how Ansible can manage Windows, the prerequisites, and use cases.
Can Ansible Manage Windows?
Yes, Ansible can manage Windows systems using WinRM (Windows Remote Management) or SSH. With its agentless architecture, Ansible performs tasks like software deployment, configuration management, and system updates on Windows nodes.
Prerequisites for Managing Windows with Ansible
1. Enable WinRM on Windows Hosts
WinRM allows Ansible to communicate with Windows machines remotely.
#### Steps to Enable WinRM:
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 Ansible Control Node
Install the pywinrm library to enable WinRM communication:
`bash
pip install pywinrm
`
3. Configure Inventory for Windows
Define the Windows hosts in your inventory file:
`ini
[windows]
windows_host ansible_host=192.168.1.10 ansible_user=Administrator ansible_password=your_password ansible_connection=winrm
`
Ansible Modules for Windows Automation
Ansible provides several modules specifically for managing Windows systems. Here are some commonly used ones:
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:
Manage user accounts.
`yaml
- name: Create 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
``
Use Cases for Ansible on Windows
1. Application Deployment:
Automate the installation and configuration of software.
2. System Configuration:
Apply consistent configurations across multiple Windows machines.
3. Service Management:
Start, stop, or restart Windows services as needed.
4. File and Directory Management:
Create, delete, or manage file permissions on Windows systems.
5. User Management:
Add, update, or remove users and groups.