Install Ansible in WSL
The Windows Subsystem for Linux (WSL) is not officially supported by Ansible and should not be used for production.
$ sudo apt update && sudo apt upgrade -y
$ sudo apt install python3-pip git libffi-dev libssl-dev -y
$ pip3 install --user ansible pywinrm # pywinrm is a Python client for the Windows Remote Management (WinRM) service
Checkout pywinrm
To restart the running WSL instance, run the following commands. Unfortunately, there is no --restart
flag just yet.
> wsl --shutdown
> wsl
Skip this step if you sticked with the recommended option
$ pip3 uninstall ansible -y
$ git clone https://github.com/ansible/ansible.git
$ source ansible/hacking/env-setup
To enable Ansible on login, run the following: (Works when installed from source ONLY)
$ echo '. ~/ansible/hacking/env-setup -q' >> ~/.bashrc
Once the WSL window is opened, run:
$ ansible --version
ansible [core 2.11.6]
config file = /home/username/.ansible.cfg
configured module search path = ['/home/username/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/username/.local/lib/python3.8/site-packages/ansible
ansible collection location = /home/username/.ansible/collections:/usr/share/ansible/collections
executable location = /home/username/.local/bin/ansible
python version = 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0]
jinja version = 3.0.2
libyaml = True
$ cd ~
$ touch .ansible.cfg && sudo nano .ansible.cfg
Paste the following:
[defaults]
inventory = ~/.ansible-hosts # used to be hostfile in older versions, but now changed to inventory
Then click CTRL+O
and Return
to save, and CTRL+X
to exit
$ cd ~
$ touch .ansible-hosts && sudo nano .ansible-hosts
Paste the following:
localhost ansible_connection=local
Then click CTRL+O
and Return
to save, and CTRL+X
to exit
$ cd ~
$ touch test.yml && sudo nano test.yml
Paste the following:
---
- name: talk to localhost
hosts: localhost
connection: local
gather_facts: no # stop gathering facts for now
tasks:
- name: Print Hello from Ansible
debug: msg='Hello from Ansible'
Then click CTRL+O
and Return
to save, and CTRL+X
to exit
Run the playbook simply by typing:
$ ansible-playbook test.yml
PLAY [talk to localhost] **********************************************************************************************
TASK [Print Hello from Ansible] *********************************************************************************************
ok: [localhost] => {
"msg": "Hello from Ansible"
}
PLAY RECAP ************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
From my own experience, Ansible works great so far and I have no issues with WSL1 and WSL2. But I didn't use it in production.