6

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

Hua
  • 73
  • 1
  • 1
  • 6
  • Could you try `python --version` and `ansible --version | grep "python version"` and check if the versions are the same? There is [similar issue](https://stackoverflow.com/questions/59304938/cannot-execute-k8s-module) 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](https://cinhtau.net/2019/02/14/ansible-openshift-python/) for that? Let me know if that help. – Jakub Mar 26 '20 at 13:30
  • You need to install the `openshift` Python module dependency for the Ansible `k8s` module. – Matthew Schuchard Mar 26 '20 at 13:42
  • 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 (https://cinhtau.net/2019/02/14/ansible-openshift-python/)! – Hua Mar 26 '20 at 22:18
  • 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 openshift` – Hua Mar 26 '20 at 22:24
  • 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. – Jeter-work Mar 28 '21 at 00:30

4 Answers4

5

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 
Koshur
  • 378
  • 1
  • 6
  • 20
4

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
Praveen Kumar K S
  • 3,024
  • 1
  • 24
  • 31
  • 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.0` – Hua Mar 26 '20 at 22:13
  • 1
    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 ```, 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. – Bruno B. Carvalho Mar 27 '20 at 10:17
1

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

Anton Smolkov
  • 136
  • 1
  • 5
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.

René Pardon
  • 187
  • 4
  • 12