-1

I've create the following task in my ansible playbook.

- name: Create a k8s namespace
      k8s: 
        state: present
        definition:
          apiVersion: v1
          kind: Secret
          metadata:
            name: bigip-login
            namespace: kube-system
          data:
            password: dGVzdA==
            username: YWRtaW4=
          type: Opaque

However when I run my playbook I got the following error:

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_k8s_payload_n071fcyu/ansible_k8s_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/common.py", line 92, in <module>
    from kubernetes.dynamic.resource import ResourceInstance
ModuleNotFoundError: No module named 'kubernetes'
fatal: [master.madebeen.com]: 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,
            "continue_on_error": false,
            "definition": {
                "apiVersion": "v1",
                "data": {
                    "password": "VGFyLk1pZC5GdW4tNDU2",
                    "username": "YWRtaW4="
                },
                "kind": "Secret",
                "metadata": {
                    "name": "bigip-login",
                    "namespace": "kube-system"
                },
                "type": "Opaque"
            },
            "delete_options": null,
            "force": false,
            "host": null,
            "kind": null,
            "kubeconfig": null,
            "label_selectors": null,
            "merge_type": null,
            "name": null,
            "namespace": null,
            "password": null,
            "persist_config": null,
            "proxy": null,
            "proxy_headers": null,
            "resource_definition": {
                "apiVersion": "v1",
                "data": {
                    "password": "VGFyLk1pZC5GdW4tNDU2",
                    "username": "YWRtaW4="
                },
                "kind": "Secret",
                "metadata": {
                    "name": "bigip-login",
                    "namespace": "kube-system"
                },
                "type": "Opaque"
            },
            "src": null,
            "state": "present",
            "template": null,
            "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 (kubernetes) on master's Python /usr/bin/python3. Please read the module documentation and install it 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"

According to the example provided here that should've worked. I have also tried the following suggested (without any success) due to not having the json file provided here as an example:

---
  apiVersion: v1
  data:
    password: dGVzdA==
    username: YWRtaW4=
  kind: Secret
  metadata:
    name: bigip-login
    namespace: kube-system
  type: Opaque

What intrigues me is the fact that both community/core kubernetes versions are currently installed:

marlon@ansible:~/.ansible$ ansible-galaxy collection install community.kubernetes
Process install dependency map
Starting collection install process
Skipping 'community.kubernetes' as it is already installed
marlon@ansible:~/.ansible$ ansible-galaxy collection install kubernetes.core
Process install dependency map
Starting collection install process
Skipping 'kubernetes.core' as it is already installed
marlon@ansible:~/.ansible$

Here is my python version that ansible is currently using:

    marlon@ansible:~$ python3 --version
Python 3.8.10
marlon@ansible:~$ ansible --version | grep "python version"
  python version = 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0]
marlon@ansible:~$

Installed ubuntu like recommended on ansible installation file:

$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible

Do you have any suggestions for use cases 1 and 2 so we can once and for all leave it here for future reference for others to benefit from them?

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
  • Could you try `python --version` and `ansible --version | grep "python version"` and check if the versions are the same? Please paste results from `python --version` command to the question. – Mikołaj Głodziak Oct 12 '21 at 09:16
  • @MikołajGłodziak here it goes. any help or pointers will be tremendously appreciated marlon@ansible:~$ python3 --version Python 3.8.10 marlon@ansible:~$ ansible --version | grep "python version" python version = 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0] marlon@ansible:~$ – Marlon Goncalves Oct 12 '21 at 13:28
  • Did you try to install `kubernetes` module? Try `pip install kubernetes` and let me know it was helpful. – Mikołaj Głodziak Oct 13 '21 at 14:00
  • @MikołajGłodziak just did it now and unfortunately the same thing man... wow incredible... "msg": "Failed to import the required Python library (kubernetes) on master's Python /usr/bin/python3. Please read the module documentation and install it 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" – Marlon Goncalves Oct 13 '21 at 15:41
  • Which system image did you use? Did you create it by yourself? Did you try to add this module to the base image? – Mikołaj Głodziak Oct 14 '21 at 13:08

1 Answers1

1

This error

"Failed to import the required Python library (kubernetes) on master's Python /usr/bin/python3.

means you don't have kubernetes module installed. Normally you could solve this problem by executing a command

pip3 install kubernetes

However, you are using an ansible, so you will have to take a different approach. Try to add this dependency to your system image. A similar question has already been asked here. The problem was with a different module, but the procedure is the same for you as well. You can find an example system image definition here. (Note, that this guy use Python 2 and your version is Python 3).

In your situation, you will have to put the command

pip3 install kubernetes

in your system image definition. If you are using the base system image, try to create your custom by adding the line as above. This Python dependency should be coded and installed into the image before it can be used by Ansible.

Mikołaj Głodziak
  • 4,775
  • 7
  • 28