Loading...
Complicated world: Ansible, OpenShift, Python and Kubernetes

Complicated world: Ansible, OpenShift, Python and Kubernetes

Used:   python v3.71  python v2.7.15  openshift-lib v0.8.4  openshift v3.11.0+cbab8ee-94  ansible v2.7.5 

I was trying to run an Ansible playbook for an OpenShift deployment on my MacBook Pro. The Ansible run crashed, and the fatal error message was:

"This module requires the OpenShift Python client. Try `pip install openshift`"

Diagnosis

This message was surprising since I knew I had it installed

pip list  | grep openshift
openshift           0.8.4

So what is wrong?

I am using Python 3.

python --version
Python 3.7.1

Ansible comes with its own python environment.

ansible --version
ansible 2.7.5
  config file = /Users/vinh/.ansible.cfg
  configured module search path = ['/Users/vinh/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.7.2 (default, Feb 12 2019, 08:15:36) [Clang 10.0.0 (clang-1000.11.45.5)]

After digging and searching, I found this GitHub issue for Kubernetes. OpenShift uses Kubernetes (k8s). Python 3 and the openshift library don’t work well. To make it work, I have to use Python 2. Maintenance and updates for Python 2.7 will stop past 2020.

Solution

Multiple Python environments are always challenging. Luckily there is a solution that worked for me before in a data science project. I use miniconda to accomplish that. Miniconda is a smaller version of anaconda. Anaconda is a free and open-source distribution of the Python and R programming languages for scientific computing (data science, machine learning applications, large-scale data processing, predictive analytics, etc.), that aims to simplify package management and deployment. The package management system conda quickly installs, runs and updates packages and their dependencies.

I create a new environment for Ansible and OpenShift.

conda create -n openshift python=2

Activate the environment, install the openshift library and exit that environment.

source activate openshift
# check python version
python --version
Python 2.7.15 :: Anaconda, Inc.
# install
pip install openshift
source deactivate

In the ansible-playbook, we can now add these lines to tell ansible to use the respective environment and check for the openshift dependency.

- hosts: localhost
  vars:
    ansible_python_interpreter: '/miniconda3/envs/openshift/bin/python'
  tasks:
    - python_requirements_facts:
        dependencies:
          - openshift

After these changes, I could clean up my global python environment.

pip uninstall openshift

Summary

A virtual environment is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects. From Ansible, OpenShift, Kubernetes and Python to miniconda, deployment has really become challenging and complicated.