Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions plugins/connection/kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,37 @@
aliases: [ kubectl_verify_ssl ]
"""

# example taken and adapted from
# https://github.com/geerlingguy/ansible-for-devops/blob/master/kubernetes/examples/kubectl-connection.yml
#
EXAMPLES = r"""
# This playbook assumes you already have the kubectl binary installed
# and available in the $PATH.
- hosts: k8s-master
become: yes
tasks:
- name: Get the phpmyadmin Pod name.
command: >
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @tpo. Instead of using kubectl here, can you instead use the kubernetes.core.k8s_info module?

Copy link
Author

@tpo tpo Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gravesm wrote:

Instead of using kubectl here, can you instead use the kubernetes.core.k8s_info module?

I'd like to. I just spent 90 minutes trying to get a simple k8s_info task to run to no avail. Same as with the kubectl plugin: the examples in the k8s_info docu are too dire to allow me to get it to work.

  • Either k8s_info tries to first connect to the master node via ssh and access the k8s API there, which fails, because the credentials to access the API are not available on the node because my management machine is my own PC and not the master node.
  • Or I am trying to get accessing the API from my local PC to work:
- hosts: master-node
  tasks:
    - local_action:
        module: k8s_info
        kind: Pod
        context: my_cluster
        # the following line doesn't seem to have any impact -
        # ansible will still try to connect to the k8s API on localhost
        # kubeconfig: /home/myself/.kube/config
      become: no
      register: pod_list
    - debug: var=pod_list

but am failing because for whatever reason, k8s_info is not accessing my default ~/.kube/config but instead trying to connect to the k8s API at localhost, which obviously fails, because localhost is not in fact a k8s cluster. Nota bene I'm running ansible 2.10 on Debian bullseye here.

So for the time being I'm stopping trying to get k8s_info to work and instead returning to my usual paid job duties again.

That means there are the following ways forward for me with this ticket:

  • a) I get help on how to get k8s_info to work
  • b) at some undefined time in the future I again take some time off from my normal schedule and try to resolve this twisted k8s_info mystery
  • c) we leave the example as is
  • d) I drop the retrieval of pod names from the example (which will obviously make it much less useful - because pod names are usually ephemeral) and put a static pod name into the example instead
  • e) we scratch this pull request
  • f) some other idea

What do you think? Do you have help or a suggestion on how to proceed?

Copy link
Author

@tpo tpo Dec 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it seems that I am unable to reserve myself time for option b) above during the coming 3 weeks or so. Any opinions on unstalling this pull request with either choice of a) c) d) e) or f) from above?

kubectl --no-headers=true get pod -l app=phpmyadmin
-o custom-columns=:metadata.name
register: phpmyadmin_pod
- name: Add the phpmyadmin Pod to the inventory.
add_host:
name: '{{ phpmyadmin_pod.stdout }}'
ansible_kubectl_namespace: default
ansible_connection: kubectl
# Note: Python is required to use other modules.
- name: Run a command inside the container.
shell: date
register: date_output
delegate_to: '{{ phpmyadmin_pod.stdout }}'
- debug: var=date_output.stdout
"""

import distutils.spawn
import os
import os.path
Expand Down