π§° Configure WSL2 as an Ansible Control Node Runbook
π― Purpose
Provision WSL2 (Ubuntu 24.04) on a Windows workstation and configure it as an Ansible control node capable of managing Linux and Windows hosts.
π§© Prerequisites
- Windows 11 workstation
- Administrator access
- Virtualization enabled
- Piβhole or equivalent DNS (optional but recommended)
- Existing Ansible control node (for remote configuration steps)
ποΈ Install WSL2
Steps
- Open PowerShell as Administrator
-
Install Ubuntu 24.04:
wsl --install -d Ubuntu-24.04 - When prompted:
- Set default user to ansible
- Set password
βIMPORTANT The
ansibleuser is required for automated configuration.
- Reboot if prompted
Optional β Change Default User Later
ubuntu config --default-user <username>
wsl --shutdown
wsl -d Ubuntu-24.04
π Launch WSL2
Steps
- Open Windows Terminal
-
Start Ubuntu:
wsl -d Ubuntu-24.04
π Update Linux Packages
Steps
Run:
sudo apt update && sudo apt upgrade -y
π Configure Passwordless Sudo
Steps
-
Create sudoers file:
sudo vi /etc/sudoers.d/99_ansible -
Add:
%ansible ALL=(ALL) NOPASSWD:ALL -
Save and exit
π·οΈ Set WSL Hostname
Steps
-
Edit config:
sudo vi /etc/wsl.conf -
Add:
[network] hostname = dev-0 generateHosts = false -
From Windows PowerShell:
wsl --shutdown -
Restart WSL
π Enable External SSH Access
Get WSL IP Address
wsl ip -4 -o addr show eth0 | awk '{print $4}' | cut -d/ -f1
Configure Port Forwarding
Replace wsl_ip_address:
netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=wsl_ip_address
βIMPORTANT SSH should now be reachable using the Windows host IP.
π Add Host to DNS (Piβhole)
Steps
- Open Piβhole β Settings β Local DNS Records
- Add:
- Domain: WSL hostname (e.g.,
dev-0) - IP: Windows host IP
- Domain: WSL hostname (e.g.,
- Save record
Verify
nslookup dev-0
βοΈ Update Ansible Inventory
Add to global_ip_addresses
Edit roles/global/vars/main.yml:
global_ip_addresses:
dev-0: 192.168.2.120
Add to Inventory Group
Edit inventory/ansible/inventory.ini:
[wsl]
dev-0
π οΈ Configure WSL Host Using Ansible
βIMPORTANT Confirm SSH access to the WSL host using the Windows host IP.
Bootstrap Python 3
ansible-playbook -k -i inventory/ansible/inventory.ini playbooks/python/bootstrap_python3.yml -u ansible -l dev-0
Join Active Directory Domain
ansible-playbook -k -i inventory/ansible/inventory.ini playbooks/ad/join_domain.yml -u ansible -l dev-0
β οΈ WARNING This playbook reboots WSL.
After reboot:
- Start WSL manually
- Reconfigure Windows port forwarding
Prep Ansible Node
ansible-playbook -k -i inventory/ansible/inventory.ini playbooks/ansible/prep_ansible_node.yml -u ansible -l dev-0
Deploy Ansible
ansible-playbook -k -i inventory/ansible/inventory.ini playbooks/ansible/deploy_ansible.yml -u ansible -l dev-0
π Verify Ansible Installation
Steps
-
Activate Python environment:
source /opt/python3.12/bin/activate -
Check version:
ansible --version
Expected Output (Example)
ansible [core 2.19.5]
ansible python module location = /opt/python_3.12/lib/python3.12/site-packages/ansible
python version = 3.12.3
...
π Working With Repositories
Option A β Use Windows Repo
cd /mnt/c/Users/<username>/GitHub/<repo>
Option B β Clone Into WSL (Recommended)
git clone https://github.com/<your-repo> ~/projects/<repo>
Benefits
- Faster filesystem performance
- Avoids CRLF issues
- Cleaner environment separation
π₯οΈ VS Code Integration (WSL)
Steps
- Install WSL extension in VS Code
- Open repo
- Open Remote Explorer β WSL Targets
- Select
Ubuntu-24.04 - Click Open Folder
VS Code now uses:
- WSL Python interpreter
- WSL Ansible extension
- WSL linting and execution