拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 从kubectl命令输出中过滤掉特定pod名称的Ansible任务

从kubectl命令输出中过滤掉特定pod名称的Ansible任务

白鹭 - 2022-03-03 2076 0 0

我正在尝试创建一个 ansible playbook 以从 k8s 默认命名空间中洗掉特定的 pod。

有2个串列:

  • pod_list(使用kubectl get pods命令输出动态创建
  • pod_filter(预定义的正则表达式,或者只是 pod 名称的开头)

这个想法是遍历pod_list并过滤掉与 中指定的模式匹配的名称pod_filter

剧本:

---
- name: Delete specific pods
  hosts: localhost

  vars:
    pod_filter:
      - hello # beginning of the pod name
      - mysql # beginning of the pod name
      - myui-557f994996-5vp8l # exact pod name

  tasks:
    - name: Get a list of all pods from the namespace
      #command: kubectl get pods --output=jsonpath='{.items[*].metadata.name}'  # Output is a single line
      command: kubectl get pods --no-headers -o custom-columns=":metadata.name" # Output is a column
      register: pod_list     

    - name: Iterate over pod names
      debug:
        msg: "{{ item }}"  
      # command: kubectl delete pod "{{ item }}"
      # pod_list.stdout_lines is a list, but pod_list.stdout is not.
      loop: "{{ pod_list.stdout_lines }}"
      when: item in pod_filter

输出如下:

$ ansible-playbook test.yaml

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Delete specific pods] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************
ok: [localhost]

TASK [Get a list of all pods from the namespace] ***********************************************************************************************************
changed: [localhost]

TASK [Iterate over pod names] ******************************************************************************************************************************
skipping: [localhost] => (item=hello-768bb8fcd-qptnl)
skipping: [localhost] => (item=mysql-66cdf96976-9jg2v)
ok: [localhost] => (item=myui-557f994996-5vp8l) => {
    "msg": "myui-557f994996-5vp8l"
}

PLAY RECAP *************************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

when: item in pod_filter只有在pod_filter串列中指定了确切的 pod 名称时,该条件才会成功如果我只指定pod_filter串列中pod 名称的一部分(开始),它就不起作用

关于测验的 Ansible 档案提到match

when为此目的应指定什么条件?是否应该有一个嵌套回圈遍历两个串列?

uj5u.com热心网友回复:

条件应该是:when: item is match(pod_filter|join('|'))这将有效地cat将您的正则表达式合二为一。

然后任务将如下所示:

- name: Iterate over pod names and delete the filtered ones
  # debug:
  #   msg: "{{ item }}"  
  command: kubectl delete pod "{{ item }}"
  loop: "{{ pod_list.stdout_lines }}"
  when: item is match(pod_filter|join('|'))

uj5u.com热心网友回复:

除了 AnjanaAK 的解决方案,您还可以使用 Kubernetes 集合

https://docs.ansible.com/ansible/latest/collections/kubernetes/core/k8s_info_module.html#ansible-collections-kubernetes-core-k8s-info-module

然后使用类似 kubectl 的过滤。档案中提供的示例之一:

- name: Search for all Pods labelled app=web
  kubernetes.core.k8s_info:
    kind: Pod
    label_selectors:
      - app = web
      - tier in (dev, test)
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *