Ansible is a powerful automation tool widely used for configuration management, application deployment, and server orchestration. While it’s commonly associated with managing Linux environments, Ansible can also be used to manage Windows servers. This guide will walk you through the process of how to connect to Windows server from Linux using Ansible on Ubuntu 2022.
Why Use Ansible to Connect to a Windows Server?
Ansible is agentless, meaning no additional software needs to be installed on the target Windows server. This makes it ideal for cross-platform management tasks. With Ansible, you can:
- Automate configuration management tasks.
- Deploy software to Windows servers.
- Execute scripts or commands remotely on Windows machines.
Prerequisites
Before proceeding, ensure the following requirements are met:
On the Ubuntu (Control Machine):
- Ubuntu 20.04/22.04 or another recent version is installed.
- Ansible is installed and configured.
- Python 3.8+ is installed (Ansible depends on Python).
On the Windows Server:
- Remote Management features, such as PowerShell Remoting, are enabled.
- The Windows server firewall allows inbound connections for WinRM (Windows Remote Management).
- You have administrative credentials for the Windows server.
Step-by-Step Guide How to Connect to Windows Server from Linux Using Ansible on Ubuntu 2022
Step 1: Install Ansible on Ubuntu
If Ansible is not already installed, you can do so by running the following commands:
Verify the installation by checking the version:
Step 2: Install Python Modules
Ansible requires specific Python modules to connect to Windows servers. Install them using the following command:
Step 3: Configure the Windows Server for WinRM
WinRM (Windows Remote Management) is required for Ansible to communicate with the Windows server. Follow these steps:
- Enable WinRM
Run the following commands in a PowerShell terminal on the Windows server:Note: Allowing unencrypted connections is suitable for testing but not recommended for production environments. Use a secure connection in production.
- Enable PowerShell Remoting
Execute: - Adjust the Firewall
Allow WinRM traffic through the firewall:
Step 4: Configure Ansible Inventory File
Ansible requires an inventory file to specify the target servers. Create or edit the inventory file, usually located at /etc/ansible/hosts.
Add the Windows server details:
Replace:
192.168.1.100with your Windows server’s IP address.Administratorandyour_passwordwith the appropriate username and password.
Step 5: Test the Connection
Run the following command to test the connection:
If the connection is successful, you’ll see output similar to:
Step 6: Write Ansible Playbooks
Ansible uses playbooks to define tasks for automation. Create a playbook to perform tasks on the Windows server. For example:
Example Playbook: Installing a Windows Feature
Create a file called install_feature.yml:
Run the playbook:
Step 7: Automate File Transfers
Ansible can also used to transfer files to and from the Windows server.
Example Playbook: Transfer a File to Windows Server
Create a file called transfer_file.yml:
Run the playbook:
Step 8: Automate Windows Updates
You can automate Windows updates using the win_updates module. Here’s an example:
Example Playbook: Automating Windows Updates
Create a file called update_windows.yml:
Run the playbook:
Best Practices
- Secure WinRM Connections
Use HTTPS instead of HTTP for secure communication. You can configure this by setting up an SSL certificate on the Windows server. - Centralized Credential Management
Use Ansible Vault to securely store sensitive information, such as passwords: - Test in a Safe Environment
Always test playbooks in a development environment before deploying them in production.
Troubleshooting Tips
- Connection Issues
- Verify that the firewall allows WinRM traffic.
- Ensure that the correct IP address and credentials are specified in the inventory file.
- Module Not Found Errors
- Ensure that the required Python modules (
pywinrm) are installed.
- Ensure that the required Python modules (
- WinRM Configuration Errors
- Re-run
winrm quickconfigon the Windows server to reset the configuration.
- Re-run
Conclusion
Using Ansible on Ubuntu to connect to and manage a Windows server provides a powerful and efficient way to perform cross-platform server administration. By following this guide, you can set up and use Ansible to automate tasks, transfer files, and manage Windows features seamlessly. Whether you’re a system administrator or a DevOps engineer, Ansible bridges the gap between Linux and Windows environments, making server management more streamlined.



