Windows as Ansible control host in WSL2

We all want to work smarter, not harder. (Or at least some of us…) Automating the repetitive tasks was always a huge leap forward even before the “DevOps times” when we wrote shell scripts and Perl code to create automation out of the box. Today we have the luxury of choosing between a lot of automation and orchestration frameworks.

In my personal opinion Ansible is one of the best tools to start with. It is easy to adopt as a beginner and we can periodically scale it as we travel on our automation journey. Ansible started as some Python libraries and wrappers when I first looked at it, and today it is the most popular choice of the IT engineers.

It is not surprising as we can build a whole automation empire on this tiny core framework.

Back in the time it was a privilege to the Linux servers to be a host to Ansible. We called it the control node. I have seen different installations and scenarios, but the control node was always a Linux server.

With the maturity of the Windows Subsystem for Linux it became possible to run Ansible on a Microsoft Windows machine. The Microsoft’s integrations and support of Linux opens up a brand new world of possibilities.

We can install Ansible in a WSL distro in multiple ways today.

According to the official documentation:

The Windows Subsystem for Linux is not supported by Ansible and should not be used for production systems.

After reading and understanding the above statement only use this method on your own risk!

Installing Ansible with the package manager

The easiest and fastest way to install Ansible on WSL is using the package manager of the distribution. If we are lucky there is no need for tweaking or adding extra repositories at all. I use Debian Linux so my example will stick to APT and the default Debian Linux repositories.

With the following command our system is immediately ready to run Ansible and Playbooks after the installation:

sudo apt install ansible

The level of the configuration and tweaking is up to us from this point.

Installing Ansible with Python pip

Installing Ansible from pip is a bit more work but it gives us the latest and greatest version of the software.

For this task I prefer Python3 and a separate Python venv. Let’s see how can we create one!

┌──(tmolnar@DebTop)-[~/stuff]
└─$ python3 -m venv python_venv

┌──(tmolnar@DebTop)-[~/stuff]
└─$ source python_venv/bin/activate

After creating and activating the venv we can install the latest Ansible package from PyPi.

┌──(python_venv)─(tmolnar@DebTop)-[~/stuff]
└─$ pip install ansible
Collecting ansible
(...)

┌──(python_venv)─(tmolnar@DebTop)-[~/stuff]
└─$ which ansible
/home/tmolnar/stuff/python_venv/bin/ansible

Now Ansible is installed in our separate, little Python environment and we can use it right out of the box.

┌──(python_venv)─(tmolnar@DebTop)-[~/stuff]
└─$ ansible localhost -m ping
[WARNING]: No inventory was parsed, only implicit localhost is available
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

We have a working Ansible control host installation in WSL on a Microsoft Windows host!

When we want to deactivate the virtual environment we just simply issue the deactivate command:

┌──(python_venv)─(tmolnar@DebTop)-[~/stuff]
└─$ deactivate

Always remember: this is not a supported way by Red Hat of installing an Ansible control node!

I automate my personal learning and practicing lab with using Ansible in WSL, and I have never ran into any issues with it.

If you have anything to share then please visit my Tom’s IT Cafe Discord Server!

4 thoughts on “Windows as Ansible control host in WSL2

Leave a reply to Private Proxies Paypal Cancel reply