"Failed to import the required Python library (kubernetes)" despite the kubernetes module being installed

2.9k Views Asked by At

I'm running Ansible on macOS 12.4 and I have following task which uses kubernetes.core.k8s module.

- name: "apply {{ downloaded_metallb_manifest }}"
  kubernetes.core.k8s:
    src: "{{ downloaded_metallb_manifest }}"
    state: present
    wait: yes

I installed the collection as per documentation - https://docs.ansible.com/ansible/latest/collections/kubernetes/core/k8s_module.html

% ansible-galaxy collection install kubernetes.core               
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/download/kubernetes-core-2.3.2.tar.gz to /Users/grit/.ansible/tmp/ansible-local-811952kgpja6g/tmpc_mrql42/kubernetes-core-2.3.2-c3aeycar
Installing 'kubernetes.core:2.3.2' to '/Users/grit/.ansible/collections/ansible_collections/kubernetes/core'
kubernetes.core:2.3.2 was installed successfully

However, the task still fails.

TASK [metallb : apply /tmp/metallb-native.yaml] ****************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'kubernetes'
fatal: [node1.local]: FAILED! => {"changed": false, "error": "No module named 'kubernetes'", "msg": "Failed to import the required Python library (kubernetes) on node1'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"}

Running the playbook with explicit -e 'ansible_python_interpreter=/usr/bin/python3' doesn't make any difference.

What went wrong here?

Here is Ansible version.

ansible [core 2.13.1]
  config file = None
  configured module search path = ['/Users/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/6.1.0/libexec/lib/python3.10/site-packages/ansible
  ansible collection location = /Users/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.10.5 (main, Jun 23 2022, 17:15:25) [Clang 13.1.6 (clang-1316.0.21.2.5)]
  jinja version = 3.1.2
  libyaml = True

The kubernetes collection is in the ansible collection location

% ls -l /Users/user/.ansible/collections/ansible_collections 
total 0
drwxr-xr-x  3 user  staff  96 26 Aug 12:57 kubernetes
drwxr-xr-x  3 user  staff  96 26 Aug 12:57 kubernetes.core-2.3.2.info

Following prerequisites have been installed as well.

% pip3 list | egrep -i 'kubernetes|pyyaml|jsonpatch'
jsonpatch           1.32
kubernetes          24.2.0
PyYAML              6.0
1

There are 1 best solutions below

0
learnd On

This is sorted by installing these libraries on target machine via pip3.

jsonpatch           1.32
kubernetes          24.2.0
PyYAML              6.0