Ansible K8s module: Failed to import the required Python library (openshift) on Python /usr/bin/python3
Asked Answered
P

4

8

The env

Ansible 2.9.6 (python3)

Tried to run a simple playbook

- hosts: master
  gather_facts: no
  become: yes
  tasks:
    - name: create name space
      k8s:
        name: testing
        api_version: v1
        kind: Namespace
        state: present

Getting following error

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_k8s_payload_u121g92v/ansible_k8s_payload.zip/ansible/module_utils/k8s/common.py", line 33, in <module>
    import kubernetes
ModuleNotFoundError: No module named 'kubernetes'
fatal: [192.168.20.38]: FAILED! => {
    "changed": false,
    "error": "No module named 'kubernetes'",
    "invocation": {
        "module_args": {
            "api_key": null,
            "api_version": "v1",
            "append_hash": false,
            "apply": false,
            "ca_cert": null,
            "client_cert": null,
            "client_key": null,
            "context": null,
            "force": false,
            "host": null,
            "kind": "Namespace",
            "kubeconfig": null,
            "merge_type": null,
            "name": "testing",
            "namespace": null,
            "password": null,
            "proxy": null,
            "resource_definition": null,
            "src": null,
            "state": "present",
            "username": null,
            "validate": null,
            "validate_certs": null,
            "wait": false,
            "wait_condition": null,
            "wait_sleep": 5,
            "wait_timeout": 120
        }
    },
    "msg": "Failed to import the required Python library (openshift) on k8smasternode's Python /usr/bin/python3. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"
}

It confuses me that,

  • the root cause is "no module named kubernetes"?
  • or "Failed to import the required Python library (openshift) on Python /usr/bin/python3"?

And how to fix that?

Any help would be appreciated!

btw,

Kubernetes master node has /usr/bin/python3

Painting answered 26/3, 2020 at 11:58 Comment(5)
Could you try python --version and ansible --version | grep "python version" and check if the versions are the same? There is similar issue on stackoverflow where community member fixed it with sudo pip install --upgrade --user openshift, worth to try it. Additionally maybe this could work as workaround for that? Let me know if that help.Viipuri
You need to install the openshift Python module dependency for the Ansible k8s module.Television
Hi @jt97, (1) python --version is Python 2.7.17 ; ansible --version | grep "python version" is python version = 3.6.9. Because there are 2 version Pythons are installed. is that an issue? (2) I've tried sudo pip install --upgrade --user openshift but still same error (3) I haven't tried the workaround yet (cinhtau.net/2019/02/14/ansible-openshift-python)!Painting
Hi @MattSchuchard, yes, I've tried a few ways to do that but still no luck. pip install openshift pyyaml kubernetes and sudo pip install --upgrade --user openshiftPainting
The command python3 --version should show you the version of python3 in use. also, on my example of this error, the error says the master node's name, k8smasternode in your case.Rainger
V
10

I am a bit late to the party but since I faced this today and don't see an accepted answer, I am posting what worked for me.

Since you are running the tasks on remote servers, you must have openshift, pyyaml and kubernetes installed on the remote machines for this to work.

Add below tasks prior to creating namespaces:

- name: install pre-requisites
  pip:
    name:
      - openshift
      - pyyaml
      - kubernetes 
Victual answered 14/4, 2022 at 11:3 Comment(1)
Thanks ! For this i had to sudo apt install python3-pip first, and it worked !Discursion
J
6

Taking a look at the documentation here: https://docs.ansible.com/ansible/latest/modules/k8s_module.html

Seems like you need to have:

  • python >= 2.7
  • openshift >= 0.6
  • PyYAML >= 3.11

One way to do this is:

pip install openshift pyyaml kubernetes 

Side note, I've added kubernetes here but I believe it's a dependency of openshift.

Also we can do like this as well:

pip3 install openshift pyyaml kubernetes --user
Jumble answered 26/3, 2020 at 12:37 Comment(2)
Thanks Bruno. I've done that pip3 install openshift pyyaml kubernetes but no luck still same error. here is the output Successfully installed cachetools-4.0.0 dictdiffer-0.8.1 google-auth-1.12.0 kubernetes-11.0.0 oauthlib-3.1.0 openshift-0.11.0 python-dateutil-2.8.1 python-string-utils-1.0.0 requests-oauthlib-1.3.0 rsa-4.0 ruamel.yaml-0.16.10 ruamel.yaml.clib-0.2.0 urllib3-1.25.8 websocket-client-0.57.0Painting
I am wondering if these libs should be installed on your ansible executor or your "master", as described in your hosts field. Could you do a kubectl use-context <your context>, add a "delegate_to: localhost" at the end of k8s module and try it? This is assuming your executor can connect to kube using kubectl. k8s also accepts a field called "context" which allows you to specify on what context you want to install.Jumble
B
3

Define this variable in inventory - ansible_python_interpreter: /usr/local/bin/python3, it must help ansible to choose right interpreter during local connection.

Bufflehead answered 1/9, 2020 at 9:24 Comment(0)
R
1

You're running the playbook with become: yes so the extension needs to be installed for the root user as well. Just had the same problem but sudo pip install openshift pyyaml fixed it for me.

Radiotransparent answered 7/6, 2021 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.