How to Connect to Windows Server from Linux Using Ansible on Ubuntu 2022

How to Connect to Windows Server from Linux Using Ansible on Ubuntu 2022?

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):

  1. Ubuntu 20.04/22.04 or another recent version is installed.
  2. Ansible is installed and configured.
  3. Python 3.8+ is installed (Ansible depends on Python).

On the Windows Server:

  1. Remote Management features, such as PowerShell Remoting, are enabled.
  2. The Windows server firewall allows inbound connections for WinRM (Windows Remote Management).
  3. 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:

bash
sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install -y ansible

Verify the installation by checking the version:

bash
ansible --version

Step 2: Install Python Modules

Ansible requires specific Python modules to connect to Windows servers. Install them using the following command:

bash
sudo apt install -y python3-pip
pip3 install "pywinrm>=0.3.0"

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:

  1. Enable WinRM
    Run the following commands in a PowerShell terminal on the Windows server:

    PowerShell
    winrm quickconfig
    winrm set winrm/config/service/Auth @{Basic="true"}
    winrm set winrm/config/service @{AllowUnencrypted="true"}

    Note: Allowing unencrypted connections is suitable for testing but not recommended for production environments. Use a secure connection in production.

  2. Enable PowerShell Remoting
    Execute:

    powershell
    Enable-PSRemoting -Force
  3. Adjust the Firewall
    Allow WinRM traffic through the firewall:

    powershell
    New-NetFirewallRule -Name "WinRM_HTTP" -DisplayName "WinRM HTTP" -Protocol TCP -LocalPort 5985 -Action Allow

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:

ini
[windows]
windows_server ansible_host=192.168.1.100 ansible_user=Administrator ansible_password=your_password ansible_connection=winrm ansible_winrm_transport=basic

Replace:

  • 192.168.1.100 with your Windows server’s IP address.
  • Administrator and your_password with the appropriate username and password.

Step 5: Test the Connection

Run the following command to test the connection:

bash
ansible windows -m win_ping

If the connection is successful, you’ll see output similar to:

json
windows_server | SUCCESS => {
"changed": false,
"ping": "pong"
}

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:

yaml
- name: Install Windows Features
hosts: windows
tasks:
- name: Install IIS
win_feature:
name: Web-Server
state: present

Run the playbook:

bash
ansible-playbook install_feature.yml

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:

yaml
- name: Transfer File to Windows Server
hosts: windows
tasks:
- name: Copy file to Windows
win_copy:
src: /path/to/local/file.txt
dest: C:\path\to\destination\file.txt

Run the playbook:

bash
ansible-playbook transfer_file.yml

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:

yaml
- name: Install Windows Updates
hosts: windows
tasks:
- name: Apply Windows updates
win_updates:
category_names:
- SecurityUpdates
reboot: yes

Run the playbook:

bash
ansible-playbook update_windows.yml

Best Practices

  1. 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.
  2. Centralized Credential Management
    Use Ansible Vault to securely store sensitive information, such as passwords:

    bash
    ansible-vault create secrets.yml
  3. Test in a Safe Environment
    Always test playbooks in a development environment before deploying them in production.

Troubleshooting Tips

  1. Connection Issues
    • Verify that the firewall allows WinRM traffic.
    • Ensure that the correct IP address and credentials are specified in the inventory file.
  2. Module Not Found Errors
    • Ensure that the required Python modules (pywinrm) are installed.
  3. WinRM Configuration Errors
    • Re-run winrm quickconfig on the Windows server to reset the configuration.

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.

Scroll to Top