kubernetes Ansible应该只在until循环中的所有重试完成后打印'FAILED',而不是在每次迭代时打印

tcomlyy6  于 2022-11-02  发布在  Kubernetes
关注(0)|答案(1)|浏览(332)

我有一个Ansible任务几乎与上面的答案相同:Ansible playbook wait until all pods running

- name: Wait for all control-plane pods become created
  shell: "kubectl get po --namespace=kube-system --selector tier=control-plane --output=jsonpath='{.items[*].metadata.name}'"
  register: control_plane_pods_created
  until: item in control_plane_pods_created.stdout
  retries: 10
  delay: 30
  with_items:
    - etcd
    - kube-apiserver
    - kube-controller-manager
    - kube-scheduler

- name: Wait for control-plane pods become ready
  shell: "kubectl wait --namespace=kube-system --for=condition=Ready pods --selector tier=control-plane --timeout=600s"
  register: control_plane_pods_ready

- debug: var=control_plane_pods_ready.stdout_lines

如他的示例所示,它将“FAILED”打印3次:

TASK [Wait for all control-plane pods become created]******************************
FAILED - RETRYING: Wait all control-plane pods become created (10 retries left).
FAILED - RETRYING: Wait all control-plane pods become created (9 retries left).
FAILED - RETRYING: Wait all control-plane pods become created (8 retries left).
changed: [localhost -> localhost] => (item=etcd)
changed: [localhost -> localhost] => (item=kube-apiserver)
changed: [localhost -> localhost] => (item=kube-controller-manager)
changed: [localhost -> localhost] => (item=kube-scheduler)

TASK [Wait for control-plane pods become ready]********************************
changed: [localhost -> localhost]

TASK [debug]*******************************************************************
ok: [localhost] => {
    "control_plane_pods_ready.stdout_lines": [
        "pod/etcd-localhost.localdomain condition met", 
        "pod/kube-apiserver-localhost.localdomain condition met", 
        "pod/kube-controller-manager-localhost.localdomain condition met", 
        "pod/kube-scheduler-localhost.localdomain condition met"
    ]    
}

对于我的实现,循环失败超过3次,更像是20次...所以它阻塞了我的日志...但这是预期的行为。
那么,如何才能在所有重试次数用完后才打印“失败”?
我希望我的问题是有意义的,谢谢

n6lpvg4x

n6lpvg4x1#

  • 如何在所有重试次数用完后才打印“失败-重试”?*

我知道您正在引用untilretries,并希望重试任务,直到满足条件,并且消息FAILED属于循环,而不是最终任务结果。
运行短时故障测试

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Show fail test
    shell:
      cmd: exit 1 # to fail
    register: result
    # inner loop
    until: result.rc == 0 # which will never happen
    retries: 3 # times therefore
    delay: 1 # second
    # outer
    loop: [1, 2, 3] # times over 
    failed_when: item == 3 and result.rc != 0 # on last outer loop run only
    no_log: true # for outer loop content

产生输出

PLAY [localhost]**********************************
FAILED - RETRYING: Show fail test (3 retries left).
FAILED - RETRYING: Show fail test (2 retries left).
FAILED - RETRYING: Show fail test (1 retries left).

TASK [Show fail test]*****************************
changed: [localhost] => (item=None)
FAILED - RETRYING: Show fail test (3 retries left).
FAILED - RETRYING: Show fail test (2 retries left).
FAILED - RETRYING: Show fail test (1 retries left).
changed: [localhost] => (item=None)
FAILED - RETRYING: Show fail test (3 retries left).
FAILED - RETRYING: Show fail test (2 retries left).
FAILED - RETRYING: Show fail test (1 retries left).
failed: [localhost] (item=None) => changed=true
  attempts: 3
  censored: 'the output has been hidden due to the fact that ''no_log: true'' was specified for this result'
fatal: [localhost]: FAILED! => changed=true
  censored: 'the output has been hidden due to the fact that ''no_log: true'' was specified for this result'

无论是使用定义故障还是使用no_log或限制循环输出保护敏感数据,似乎都无法在剧本级别上抑制Ansible的untilretries循环的临时消息。

  • 但这是意料之中的行为 *

对,除非你没有解决Callback plugins和设置一个(其他)回调插件的ansible-playbook或开发(自己的)回调插件的消息将保持。

类似问答

相关问题