kubernetes 如何确保使用ansible删除POD?

w8biq8rn  于 2023-05-06  发布在  Kubernetes
关注(0)|答案(3)|浏览(203)

我正在尝试验证pod是否已删除。

- name: Verify whether the POD is deleted
  command: "{{ local_kubectl }} get pod {{ pod_name}}"

但任务失败,因为命令显示如下错误
'服务器错误(NotFound):豆荚……”
但这是预期的,当我检查删除的pod。
如何在返回错误消息时传递此任务?

5m1hhzi4

5m1hhzi41#

您可以使用k8s模块直接指定此参数

- name: Delete the POD
  k8s:
    api_version: v1
    kind: Pod
    namespace: "{{ k8s_namespace }}"
    name: "{{ pod_name }}"
    state: absent

另一种方法是重新定义“failure”以检查预期的结果字符串。

- name: Verify whether the POD is deleted
  command: "{{ local_kubectl }} get pod {{ pod_name}}"
  register: verify
  failed_when: "'NotFound' not in verify.stderr"
z0qdvdin

z0qdvdin2#

尝试k8s_facts

- k8s_facts:
    kind: Pod
    name: "{{ pod_name}}"
  register: result
- debug:
    var: result

(not测试)

q5lcpyga

q5lcpyga3#

迟来的答复。希望对某人有帮助。https://docs.ansible.com/ansible/latest/collections/kubernetes/core/k8s_module.html

- name: Delete pod
  kubernetes.core.k8s:
    state: absent
    kind: Pod
    name: "{{pod_name}}"
    namespace: "{{test_ns}}"
    kubeconfig: "{{ config }}"
    wait: true
    wait_condition:
      status: False #wait for status to be false
    wait_sleep: 2  #check every 2 secs

相关问题