Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc: add example of using kubectl connection plugin #741

Merged
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- connection/kubectl.py - Added an example of using the kubectl connection plugin to the documentation (https://github.com/ansible-collections/kubernetes.core/pull/741).
78 changes: 78 additions & 0 deletions docs/kubernetes.core.kubectl_connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,84 @@ Parameters



Examples
--------

.. code-block:: yaml

- name: Run a command in a pod using local kubectl with kubeconfig file ~/.kube/config
hots: localhost
yurnov marked this conversation as resolved.
Show resolved Hide resolved
gather_facts: no
connection: kubernetes.core.kubectl
vars:
ansible_kubectl_namespace: my-namespace
ansible_kubectl_pod: my-pod
ansible_kubectl_container: my-container
tasks:
# be aware that the command is executed as the user that started the container
# and requrie python to be installed in the image
yurnov marked this conversation as resolved.
Show resolved Hide resolved
- name: Run a command in a pod
ansible.builtin.command: echo "Hello, World!"

- name: Run a command in a pod using local kebectl with inventory variables
# Example inventory:
# k8s:
# hosts:
# foo.example.com:
# ansible_connection: kubernetes.core.kubectl
# ansible_kubectl_kubeconfig: /root/.kube/foo.example.com.config
# ansible_kubectl_pod: my-foo-pod
# ansible_kubectl_container: my-foo-container
# ansible_kubectl_namespace: my-foo-namespace
# bar.example.com:
# ansible_connection: kubernetes.core.kubectl
# ansible_kubectl_kubeconfig: /root/.kube/bar.example.com.config
# ansible_kubectl_pod: my-bar-pod
# ansible_kubectl_container: my-bar-container
# ansible_kubectl_namespace: my-bar-namespace
hosts: k8s
gather_facts: no
tasks:
# be aware that the command is executed as the user that started the container
# and requrie python to be installed in the image
yurnov marked this conversation as resolved.
Show resolved Hide resolved
- name: Run a command in a pod
ansible.builtin.command: echo "Hello, World!"

- name: Run a command in a pod using dynamic inventory
hosts: localhost
gather_facts: no
vars:
kubeconfig: /root/.kube/config
namespace: my-namespace
my_app: my-app
tasks:
- name: Get My App pod info based on label
kubernetes.core.k8s_info:
kubeconfig: "{{ kubeconfig }}"
namespace: "{{ namespace }}"
kind: Pod
label_selectors: app.kubernetes.io/name = "{{ my_app }}"
register: my_app_pod

# community.general.json_query is required, plesae install it
yurnov marked this conversation as resolved.
Show resolved Hide resolved
# with `ansible-galaxy collection install community.general`
- name: Get My App pod name
ansible.builtin.set_fact:
my_app_pod_name: "{{ my_app_pod | community.general.json_query('resources[0].metadata.name') }}"

- name: Add My App pod to inventory
ansible.builtin.add_host:
name: "{{ my_app_pod_name }}"
ansible_connection: kubernetes.core.kubectl
ansible_kubectl_kubeconfig: "{{ kubeconfig }}"
ansible_kubectl_pod: "{{ my_app_pod_name }}"
ansible_kubectl_namespace: "{{ namespace }}"

- name: Run a command in My App pod
# be aware that the command is executed as the user that started the container
# and requrie python to be installed in the image
yurnov marked this conversation as resolved.
Show resolved Hide resolved
ansible.builtin.command: echo "Hello, World!"
delegate_to: "{{ my_app_pod_name }}"



Expand Down
77 changes: 77 additions & 0 deletions plugins/connection/kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,83 @@
aliases: [ kubectl_verify_ssl ]
"""

EXAMPLES = r"""

- name: Run a command in a pod using local kubectl with kubeconfig file ~/.kube/config
yurnov marked this conversation as resolved.
Show resolved Hide resolved
hots: localhost
gather_facts: no
connection: kubernetes.core.kubectl
vars:
ansible_kubectl_namespace: my-namespace
ansible_kubectl_pod: my-pod
ansible_kubectl_container: my-container
tasks:
# be aware that the command is executed as the user that started the container
# and requrie python to be installed in the image
yurnov marked this conversation as resolved.
Show resolved Hide resolved
- name: Run a command in a pod
ansible.builtin.command: echo "Hello, World!"

- name: Run a command in a pod using local kebectl with inventory variables
yurnov marked this conversation as resolved.
Show resolved Hide resolved
# Example inventory:
# k8s:
# hosts:
# foo.example.com:
# ansible_connection: kubernetes.core.kubectl
# ansible_kubectl_kubeconfig: /root/.kube/foo.example.com.config
# ansible_kubectl_pod: my-foo-pod
# ansible_kubectl_container: my-foo-container
# ansible_kubectl_namespace: my-foo-namespace
# bar.example.com:
# ansible_connection: kubernetes.core.kubectl
# ansible_kubectl_kubeconfig: /root/.kube/bar.example.com.config
# ansible_kubectl_pod: my-bar-pod
# ansible_kubectl_container: my-bar-container
# ansible_kubectl_namespace: my-bar-namespace
hosts: k8s
gather_facts: no
tasks:
# be aware that the command is executed as the user that started the container
# and requrie python to be installed in the image
yurnov marked this conversation as resolved.
Show resolved Hide resolved
- name: Run a command in a pod
ansible.builtin.command: echo "Hello, World!"

- name: Run a command in a pod using dynamic inventory
hosts: localhost
gather_facts: no
vars:
kubeconfig: /root/.kube/config
namespace: my-namespace
my_app: my-app
tasks:
- name: Get My App pod info based on label
kubernetes.core.k8s_info:
kubeconfig: "{{ kubeconfig }}"
namespace: "{{ namespace }}"
kind: Pod
label_selectors: app.kubernetes.io/name = "{{ my_app }}"
register: my_app_pod

# community.general.json_query is required, plesae install it
yurnov marked this conversation as resolved.
Show resolved Hide resolved
# with `ansible-galaxy collection install community.general`
- name: Get My App pod name
ansible.builtin.set_fact:
my_app_pod_name: "{{ my_app_pod | community.general.json_query('resources[0].metadata.name') }}"
yurnov marked this conversation as resolved.
Show resolved Hide resolved

- name: Add My App pod to inventory
ansible.builtin.add_host:
name: "{{ my_app_pod_name }}"
ansible_connection: kubernetes.core.kubectl
ansible_kubectl_kubeconfig: "{{ kubeconfig }}"
ansible_kubectl_pod: "{{ my_app_pod_name }}"
ansible_kubectl_namespace: "{{ namespace }}"

- name: Run a command in My App pod
# be aware that the command is executed as the user that started the container
# and requrie python to be installed in the image
yurnov marked this conversation as resolved.
Show resolved Hide resolved
ansible.builtin.command: echo "Hello, World!"
delegate_to: "{{ my_app_pod_name }}"
"""

import json
import os
import os.path
Expand Down
Loading