I have discussed this slightly before, however, some background may be necessary. I build a relatively well used Docker container with Ansible. Originally this was all based on Python 2 which was the default Python that is installed with most operating systems. However, this was soon to change!

Python 2.7 will reach the end of its life on January 1st, 2020

The longer message that read out when installing using Python 2 was:

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won’t be maintained after that date. A future version of pip will drop su pport for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support

So, Python 2 was disappearing. And that meant that we needed to upgrade. Additionally, one of the best issues (most complete, giving a clear objective and even a resolution) had been raised inside the GitHub project for the container python 2.7 reaches End of Life in 2 months #13.

Upgrading Ansible to Python 3

Actually, Ansible works similarly under both Python 3 and Python 2, however, it is mainly the install process that defines which version of Python Ansible will use. This breaks the process down into 2 main sections.

Install Python 3, Pip3 and Ansible

Some time ago I switched out the repo versions of installing Ansible in favour of pip. Mainly this gives not only the ability to make the process the same across all operating systems, but also ensures that you can easily install previous versions of Ansible, or specific versions if required.


Alpine, unlike many other distros, does not come with Python 3 pre-installed in the base image, therefore you need to install both Python 3 and Pip3 to be able to run both successfully.

RUN apk add --no-cache --update python3 python3-dev py3-pip

And the install of Ansible:

RUN pip3 install --upgrade pip cffi && \
    pip3 install ansible && \
    pip3 install mitogen ansible-lint && \
    pip3 install --upgrade pywinrm && \

CentOS, Debian & Ubuntu

Installing pip3 inside Centos, Debian and Ubuntu is relatively straightforward as you just need to install the package python3-pip, however, there is a caveat with this. Once you install pip3 you need to run commands with pip3, however, if you upgrade pip3 it will then replace the pip command. In an example from the Dockerfile:

RUN pip3 install --upgrade pip cffi && \
    pip install ansible==2.8.8 && \
    pip install mitogen ansible-lint && \
    pip install --upgrade pywinrm

The process for installing Ansible is pretty seamless: pip install ansible or if you wish to install a specific version then you can name that version: pip install ansible=2.8.8.

Ansible running with Python 3

We now have Ansible running under Python 3 with all of the packages being pulled by pip3 which should give much greater support going forwards.

The process for me was actually pretty seamless. The changes were minimal and mainly around package naming. I do need to do some more upgrades in the future but at the moment we are happy with that.