Ansible is a highly flexible automation tool that can manage Windows systems alongside Linux and other platforms. This article explains how Ansible enables Windows automation, its prerequisites, and key use cases.
Can Ansible Manage Windows Systems?
Yes, Ansible can manage and automate Windows systems efficiently. Using Windows Remote Management (WinRM) or SSH, Ansible interacts with Windows machines to execute tasks like configuring services, installing software, and managing files.
Setting Up Ansible for Windows
1. Enable WinRM on Windows Hosts
WinRM allows Ansible to communicate with Windows machines. To enable it:
- Open PowerShell as Administrator.
- Execute 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 Required Libraries on Ansible Control Node
Install the pywinrm library to enable WinRM communication:
pip install pywinrm
3. Configure Inventory for Windows
Add Windows hosts to the Ansible inventory file:
[windows]
windows_host ansible_host=192.168.1.10 ansible_user=Administrator ansible_password=your_password ansible_connection=winrm
Windows Modules in Ansible
Ansible provides a wide range of modules specifically for managing Windows systems. These modules simplify tasks such as service configuration, user management, and software installation.
Examples of Windows Modules
win_service
: Manage Windows services.- name: Ensure IIS service is running win_service: name: W3SVC state: started
win_package
: Install or uninstall software.- name: Install Chrome win_package: path: "https://dl.google.com/chrome/install/GoogleChromeStandaloneEnterprise.msi"
win_user
: Manage user accounts.- name: Create a new user win_user: name: dev_user password: StrongPassword123! state: present
win_shell
: Run PowerShell or command-line commands.- name: Run a PowerShell command win_shell: Get-Process
win_file
: Manage files and directories.- name: Create a directory win_file: path: C:\Temp state: directory
Use Cases for Ansible on Windows
Application Deployment: Automate software installations and updates.
Service Management: Start, stop, or restart services.
Configuration Management: Enforce consistent configurations across Windows systems.
User Management: Add, update, or remove user accounts and groups.
File Management: Manage files, directories, and permissions.
Running Ansible Playbooks for Windows
Once the inventory and playbooks are configured, run a playbook targeting Windows systems:
ansible-playbook -i inventory.ini windows-playbook.yml
Example Playbook to Configure Windows
- hosts: windows
tasks:
- name: Install IIS
win_feature:
name: Web-Server
state: present
- name: Start IIS service
win_service:
name: W3SVC
state: started
Best Practices for Windows Automation with Ansible
- Secure Credentials: Use Ansible Vault to encrypt sensitive data like passwords.
- Validate Configurations: Test playbooks in a non-production environment.
- Leverage Roles: Use roles to modularize and organize tasks.
Conclusion
Ansible is an excellent choice for managing Windows systems, offering simplicity and flexibility. With its robust set of modules and playbooks, you can automate Windows tasks alongside other platforms, streamlining your infrastructure management.
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
Explore comprehensive examples in Ansible by Examples.
Donate
Support this project and contribute today.