我正在嘗試創建一個 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:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/417270.html
標籤:
上一篇:在R中組合大量資料集
下一篇:Laravel上傳照片可選
