简体   繁体   中英

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

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 kube.netes" ?
  • 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,

Kube.netes master node has /usr/bin/python3

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 kube.netes 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 

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

在清单中定义此变量 - ansible_python_interpreter: /usr/local/bin/python3 ,它必须有助于在本地连接期间选择正确的解释器。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM