Why is ansible kubernetes not finding kube config while kubectl does

130 views Asked by At

I have a server that is running k3s and I'm trying to install a few things on it using ansible. The ansible task is this one here:

- name: Create lb
  kubernetes.core.k8s:
    state: present
    src: /opt/metallb/metallb-native.yaml

But it fails with Could not create API client: Invalid kube-config file. No configuration found.. I made sure that there is a config file in ~/.kube/config and I also don't have any problems when running:

- name: test kubectl 
  ansible.builtin.shell:
    cmd: kubectl get nodes 

The logs I get with -vvv are:

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_kubernetes.core.k8s_payload_tu6hy1j6/ansible_kubernetes.core.k8s_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/k8s/client.py", line 153, in _create_configuration
    kubernetes.config.load_incluster_config()
  File "/usr/local/lib/python3.10/dist-packages/kubernetes/config/incluster_config.py", line 121, in load_incluster_config
    try_refresh_token=try_refresh_token).load_and_set(client_configuration)
  File "/usr/local/lib/python3.10/dist-packages/kubernetes/config/incluster_config.py", line 54, in load_and_set
    self._load_config()
  File "/usr/local/lib/python3.10/dist-packages/kubernetes/config/incluster_config.py", line 62, in _load_config
    raise ConfigException("Service host/port is not set.")
kubernetes.config.config_exception.ConfigException: Service host/port is not set.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/ansible_kubernetes.core.k8s_payload_tu6hy1j6/ansible_kubernetes.core.k8s_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/k8s/client.py", line 351, in get_api_client
    configuration = _create_configuration(auth_spec)
  File "/tmp/ansible_kubernetes.core.k8s_payload_tu6hy1j6/ansible_kubernetes.core.k8s_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/k8s/client.py", line 158, in _create_configuration
    raise err
  File "/tmp/ansible_kubernetes.core.k8s_payload_tu6hy1j6/ansible_kubernetes.core.k8s_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/k8s/client.py", line 156, in _create_configuration
    _load_config(auth)
  File "/tmp/ansible_kubernetes.core.k8s_payload_tu6hy1j6/ansible_kubernetes.core.k8s_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/k8s/client.py", line 126, in _load_config
    kubernetes.config.load_kube_config(config_file=None, **optional_arg)
  File "/usr/local/lib/python3.10/dist-packages/kubernetes/config/kube_config.py", line 815, in load_kube_config
    loader = _get_kube_config_loader(
  File "/usr/local/lib/python3.10/dist-packages/kubernetes/config/kube_config.py", line 772, in _get_kube_config_loader
    raise ConfigException(
kubernetes.config.config_exception.ConfigException: Invalid kube-config file. No configuration found.

Why is the kubernetes module not able to find the configs, while running kubectl in a shell works? And how can I get the kubernetes module working?

1

There are 1 answers

0
iaquobe On

I found the problem.

In the ansible-vault I was using, become_password was set, so all tasks were executing under root. This changed the ~ directory, which no longer contained the kube config.

Why it still did not work with absolute paths I'm not sure, but setting

- name: name of task
  kubernetes.core.k8s:
    kubeconfig: "~{{ansible_user}}/.kube/config"
    ...

Will take the users config whether become set or not