如何重试可能失败的 Ansible 任务? [英] How to retry Ansible task that may fail?

查看:30
本文介绍了如何重试可能失败的 Ansible 任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Ansible 游戏中,我正在重新启动数据库,然后尝试对其进行一些操作.Restart 命令在重新启动后立即返回,而不是在 db 启动时返回.下一个命令尝试连接到数据库.当 db 未启动时,该命令失败.

In my Ansible play I am restarting database then trying to do some operations on it. Restart command returns as soon as restart is started, not when db is up. Next command tries to connect to the database. That command my fail when db is not up.

我想重试几次我的第二个命令.如果上次重试失败,我希望我的游戏失败.

I want to retry my second command a few times. If last retry fails, I want to fail my play.

当我按如下方式重试时

  retries: 3  
  delay: 5

然后根本不执行重试,因为第一个命令执行在整个播放过程中都失败了.我可以添加 ignore_errors: yes 但这样即使所有重试都失败,播放也会通过.有没有一种简单的方法可以在我成功之前重试失败,但在上次重试没有成功时失败?

Then retries are not executed at all, because first command execution fails whole play. I could add ignore_errors: yes but that way play will pass even if all retries failed. Is there a easy way to retry failures until I have success, but fail when no success from last retry?

推荐答案

我不明白您所说的第一个命令执行在整个游戏过程中失败".如果 Ansible 这样做是没有意义的.

I don't understand your claim that the "first command execution fails whole play". It wouldn't make sense if Ansible behaved this way.

以下任务:

- command: /usr/bin/false
  retries: 3
  delay: 3
  register: result
  until: result.rc == 0

产生:

TASK [command] ******************************************************************************************
FAILED - RETRYING: command (3 retries left).
FAILED - RETRYING: command (2 retries left).
FAILED - RETRYING: command (1 retries left).
fatal: [localhost]: FAILED! => {"attempts": 3, "changed": true, "cmd": ["/usr/bin/false"], "delta": "0:00:00.003883", "end": "2017-05-23 21:39:51.669623", "failed": true, "rc": 1, "start": "2017-05-23 21:39:51.665740", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

这似乎正是您想要的.

这篇关于如何重试可能失败的 Ansible 任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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