ansible wsl2 winrm example

 If you perform a google search with the following search string:

ansible wsl2 winrm example

You get the text shown below. Here's a link to the search: https://share.google/aimode/h3bamT37y4UVqIDwU

You can find your Windows username by opening a Windows command prompt. If you forget your Windows password, you can reset it here: https://account.microsoft.com/

Instead of localhost, I used the IP address of my Windows device.

Using Ansible within WSL2 to manage its Windows host via WinRM involves configuring the Windows host to accept remote management and setting up Ansible with the pywinrm library.

1. Configure the Windows Host (PowerShell)

WinRM is usually disabled or set to “Public” by default, which blocks connections. Run these commands in an Administrator PowerShell window to enable it for local management:

# Set network to Private (required for WinRM in most cases)
Set-NetConnectionProfile -NetworkCategory Private
 
# Enable WinRM with default settings
WinRM quickconfig
 
# Allow Basic authentication (for simple testing/local lab setup)
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'

Note: Using unencrypted HTTP and Basic auth is only recommended for local, isolated development environments.

2. Set Up Ansible in WSL2 (Ubuntu)

In your WSL2 terminal (e.g., Ubuntu), install Ansible and the mandatory WinRM connection library:bash# Update and install Ansible

sudo apt update && sudo apt install ansible -y
 
# Install the WinRM plugin for Python
pip install pywinrm

3. Create the Inventory File (hosts.ini)

WSL2 can reach the host at localhost or via the specific host IP. Create a file named hosts.ini:

[windows]
localhost  # Or use your machine's actual IP

[windows:vars]
ansible_user=YourWindowsUsername
ansible_password=YourWindowsPassword
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore
ansible_port=5985
ansible_winrm_transport=basic

4. Test the Connection

Run the win_ping module to verify that Ansible can communicate with Windows:

ansible windows -i hosts.ini -m win_ping

If successful, you will see a “ping”: “pong” response.

5. Example Playbook (site.yml)

You can now run tasks like creating directories or managing services:

- name: Test Windows Management
  hosts: windows
  tasks:
    - name: Ensure a directory exists
      ansible.windows.win_file:
        path: C:\AnsibleTest
        state: directory

    - name: Get system information
      ansible.windows.win_powershell:
        script: Get-Service | Where-Object { $_.Status -eq "Running" }
      register: running_services

    - name: Display running services
      debug:
        var: running_services
 

Run it with:

ansible-playbook -i hosts.ini site.yml

Results

The above procedures worked for me. Running the playbook displays all of the services running on my other Windows device in a json format. I can now connect to my Linux machines through SSH (see Running ansible playbook on wsl Ubuntu) and to my Windows device using WinRM.

Comments

Popular posts from this blog

Using Drupal and DITA OT to Create a PDF File

DokuWiki Docker Container

Favorite Docker Containers