kubernetes.core.kubectl 连接 – 在 Kubernetes 上运行的 Pod 中执行任务。
注意
此连接插件是 kubernetes.core 集合(版本 5.0.0)的一部分。
如果您使用的是 ansible 包,则可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list。
要安装它,请使用:ansible-galaxy collection install kubernetes.core。您需要其他要求才能使用此连接插件,有关详细信息,请参阅要求。
要在 playbook 中使用它,请指定:kubernetes.core.kubectl。
概要
- 使用 kubectl exec 命令在 Kubernetes 容器平台上运行的 Pod 中运行任务,或将文件放入/提取到 Pod。 
要求
本地控制器节点上需要以下要求来执行此连接。
- kubectl (go 二进制文件) 
参数
| 参数 | 注释 | 
|---|---|
| 用于与 API 进行身份验证的 CA 证书的路径。 默认:  配置 
 | |
| 用于与 API 进行身份验证的证书的路径。 默认:  配置 
 | |
| 用于与 API 进行身份验证的密钥文件的路径。 默认:  配置 
 | |
| 要传递给 kubectl 命令行的额外参数。 请注意,这会直接在命令行上传递信息,并且可能会暴露敏感数据。 默认:  配置 
 | |
| 用于访问 API 的 URL。 默认:  配置 
 | |
| kubectl 配置文件的路径。默认为~/.kube/config 该配置可以作为字典提供。在 2.4.0 版本中添加。 默认:  配置 
 | |
| 要本地传递给 kubectl 命令行的本地环境变量。 请注意,这会直接在命令行上传递信息,并且可能会暴露敏感数据。 默认:  配置 
 | |
| 提供用于与 API 进行身份验证的密码。 请注意,这会将信息直接在命令行上传递,可能会暴露敏感数据。我们建议改用基于文件的身份验证选项。 默认:  配置 
 | |
| API 身份验证持有者令牌。 请注意,这会将信息直接在命令行上传递,可能会暴露敏感数据。我们建议改用基于文件的身份验证选项。 配置 
 | |
| 提供用于与 API 进行身份验证的用户名。 默认:  配置 
 | |
| 是否验证 API 服务器的 SSL 证书。默认为true。 默认:  配置 
 | 
示例
- name: Run a command in a pod using local kubectl with kubeconfig file ~/.kube/config
  hosts: localhost
  gather_facts: no
  vars:
    ansible_connection: kubernetes.core.kubectl
    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 requires python to be installed in the image
    - name: Run a command in a pod
      ansible.builtin.command: echo "Hello, World!"
- name: Run a command in a pod using local kubectl 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 requires python to be installed in the image
    - 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
    - name: Get My App pod name
      ansible.builtin.set_fact:
        my_app_pod_name: "{{ my_app_pod.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 requires python to be installed in the image
      ansible.builtin.command: echo "Hello, World!"
      delegate_to: "{{ my_app_pod_name }}"
