问题描述
我有这个 ansible (working) playbook,它查看 kubectl get pods -o json
的输出,直到 pod 处于 Running
状态.现在我想将其扩展到多个 Pod.核心问题是kubectl查询的json结果是一个列表,我知道如何访问第一项,但不是所有的项...
I have this ansible (working) playbook that looks at the output of kubectl get pods -o json
until the pod is in the Running
state. Now I want to extend this to multiple pods. The core issue is that the json result of the kubectl query is a list, I know how to access the first item, but not all of the items...
- name: wait for pods to come up
shell: kubectl get pods -o json
register: kubectl_get_pods
until: kubectl_get_pods.stdout|from_json|json_query('items[0].status.phase') == "Running"
retries: 20
json 对象看起来像,
The json object looks like,
[ { ... "status": { "phase": "Running" } },
{ ... "status": { "phase": "Running" } },
...
]
使用 [0]
访问第一个项目,用于处理列表中的一个对象,但我不知道如何将其扩展到多个项目.我试过 [*]
没有用.
Using [0]
to access the first item worked for handling one object in the list, but I can't figure out how to extend it to multiple items. I tried [*]
which did not work.
推荐答案
我会尝试这样的事情(对我有用):
I would try something like this (works for me):
tasks:
- name: wait for pods to come up
shell: kubectl get pods -o json
register: kubectl_get_pods
until: kubectl_get_pods.stdout|from_json|json_query('items[*].status.phase')|unique == ["Running"]
您基本上是获取所有 Pod 的所有状态并将它们组合成一个唯一的列表,然后直到该列表是 ["Running"]
时它才会完成.因此,例如,如果您的所有 pod 都没有运行,您将得到类似 ["Running", "Starting"]
的信息.
You are basically getting all the statuses for all the pods and combining them into a unique list, and then it won't complete until that list is ["Running"]
. So for example, if all your pods are not running you will get something like ["Running", "Starting"]
.
这篇关于Ansible playbook 等待所有 Pod 运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!