重新运行任务如果其结果失败

huangapple go评论69阅读模式
英文:

How to re-run a task if its result fails

问题

---
- name: 获取md5
  net_command:
    commands:
      - "bash md5sum {{ image }}"
  register: md5_remote

- name: 验证md5
  assert:
    that:
      - md5_local in md5_remote.stdout[0]
  register: md5_result
  until: "md5_result is not failed"
  retries: 5
  delay: 60

请注意,net_command 是我们用于某些特定网络设备的自定义模块,没有内置的 Ansible 模块。

因此,我尝试的是,如果 Verify md5 断言失败,重新运行 Get md5 任务。目前,它只是在 Verify md5 上循环,而实际上没有重新计算检验和,所以如果它失败一次,它将永远无法完成。

所以,基本上,我需要多次重新计算检验和,直到它与存储在 md5_local 中的预期值匹配。

英文:

I have a couple of tasks like this:

---
- name: Get md5
  net_command:
    commands:
      - "bash md5sum {{ image }}"
  register: md5_remote


- name: Verify md5
  assert:
    that:
      - md5_local in md5_remote.stdout[0]
  register: md5_result
  until: "md5_result is not failed"
  retries: 5
  delay: 60

Note that net_command is a custom module we use for some specific networking equipment that does not have any builtin Ansible modules.

So what I am trying to do is to re-run the Get md5 task if the Verify md5 assertion fails. Right now it's just looping over Verify md5 without actually re-calculating the checksum again, so, if it fails once it will never work complete.

So, basically, I need to recaculate the checksum multiple times until it matches the expected one, stored in md5_local.

答案1

得分: 2

如果您想重试一个任务,您需要在该任务上使用until循环。由于until已经是一个断言,所以不需要单独的assert任务。

因此,您的用例应该可以通过单个任务来实现:

- name: 获取 md5
  net_command:
    commands:
      - "bash md5sum {{ image }}"
  register: md5_remote
  until: md5_local in md5_remote.stdout[0]
  retries: 5
  delay: 60
英文:

If you want to retry a task, you'll have to put the until loop on that task. Since until is an assertion already, there is no need for a separate assert task.

So, your use case should be covered by a single task:

- name: Get md5
  net_command:
    commands:
      - "bash md5sum {{ image }}"
  register: md5_remote
  until: md5_local in md5_remote.stdout[0]
  retries: 5
  delay: 60

huangapple
  • 本文由 发表于 2023年2月24日 01:57:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定