Skip to content

Instantly share code, notes, and snippets.

@Tes3awy
Last active February 18, 2022 19:50
Show Gist options
  • Save Tes3awy/dd73fdf0cd7e650d55a89d254a0005aa to your computer and use it in GitHub Desktop.
Save Tes3awy/dd73fdf0cd7e650d55a89d254a0005aa to your computer and use it in GitHub Desktop.
Install Ansible (ansible [core 2.11.6]) on Windows Subsystem for Linux (WSL) and run your first playbook

Install Ansible release (Recommended)

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

Restart WSL

To restart the running WSL instance, run the following commands. Unfortunately, there is no --restart flag just yet.

> wsl --shutdown
> wsl

Install Ansible from source instead of a release (Not recommended)

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

And restart WSL

Validate Ansible Installation

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

Setup Ansible to work for localhost

Create .ansible.cfg file

$ 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

Create .ansible-hosts file

$ 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

Create and run test.yml

$ 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment