Table of Content

Run ansible on WSL

what is Ansible?

basic fact:

  • open-source configuration management/deployment tool
  • written by Michael DeHaan and acquired by Red Hat in 2015
  • run as python as agentless on all Unix-like via SSH
  • not running on window but can manage window host via winrm
  • ansible playbook in YAML

why need Ansible running on WSL?

It is challenge to run Ansible on window, by nature we cannot run Ansbile on window but here are alternative ways: WSL or Msys/cygwin.

Benefits of running Ansible on WSL:

  • best Linux dev env on Window
  • workstation for docker/k8s dev deployment
  • control node to automate the configuration/deployment workflow

how to install Ansible on WSL?

1) upgrade python to at least 3.8
I found it is very hard to install python 3.8 and pip3 on WSL 18.04, so moved to WSL 20.04 then Python 3.8 is by default.

pip --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

2) install ansible using pip3

$ pip install ansible
...
Installing collected packages: pyparsing, packaging, resolvelib, ansible-core, ansible
Successfully installed ansible-4.6.0 ansible-core-2.11.5 packaging-21.0 pyparsing-2.4.7 resolvelib-0.5.4

$ pip list |grep ansible
ansible                4.6.0
ansible-core           2.11.5

One thing you cannot find ansible when installing as pip,

$ which -a ansible 

It is located at ~/.local/bin, so need to add it to $PATH

export PATH=$PATH:~/.local/bin

$ which -a ansible
/home/oldhorse/.local/bin/ansible

test Ansible on WSL

here is test case,

$ cat .ansible.cfg
[defaults]
inventory = ~/.ansible-hosts

$ cat .ansible-hosts
localhost ansible_connection=local

$ cat hello.yaml
---
- hosts: all
  tasks:
    - shell: echo 'hello world'

$ ansible-playbook hello.yaml

PLAY [all] *************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [localhost]

TASK  ***********************************************************************************************************
changed: [localhost]

PLAY RECAP *************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0