我正在使用ansible k8s模块进行pod部署,但是不幸的是,我没有找到正确使用wait
和wait_condition
的方法。
所以我尝试使用until
和retry
来实现另一个等待状态
每次运行剧本之后,使用k8s模块进行部署之后,直到NewReplicaSetAvailable在2分钟(60 * 2s)内显示在部署中,否则部署失败。
如果您需要验证部署结果,我在这里写下。
- name: Verify the deploy result
shell: "kubectl -n kube-system get deployment coredns --output=jsonpath='{.status.conditions[*].reason}'"
register: deploy_res
until: "'NewReplicaSetAvailable' in deploy_res.stdout"
retries: 60
delay: 2
答案 0 :(得分:2)
模块k8s
未提供部署状态。对于部署(或任何其他资源)的状态,应使用模块k8s_info
。然后,您可以检查此任务。
示例:
---
- hosts: localhost
tasks:
- name: Create a Service object from an inline definition
k8s:
state: present
definition:
apiVersion: v1
kind: Deployment
metadata:
name: example
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: example-nginx
template:
metadata:
labels:
app: example-nginx
spec:
containers:
- image: nginx
name: nginx
- name: check if deployment is ready
k8s_info:
kind: Deployment
label_selectors:
- app = example-nginx
register: output_info
until: output_info.resources | json_query('[*].status.conditions[?reason==`NewReplicaSetAvailable`][].status') | select ('match','True') | list | length == 1
delay: 2
retries: 5
对于until
过滤器,肯定有更好的解决方案,但是对于类似的用例,我已经用这种方法解决了。 until
过滤器检查是否存在原因为NewReplicaSetAvailable
的条件,且条件必须为True
。如果发生这种情况,则长度等于1