Ansible剧本等到所有Pod都在运行 [英] Ansible playbook wait until all pods running

查看:197
本文介绍了Ansible剧本等到所有Pod都在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的(工作中)剧本,它查看kubectl get pods -o json的输出,直到Pod处于Running状态.现在,我想将其扩展到多个吊舱.核心问题是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剧本等到所有Pod都在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆