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

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

问题描述

在Ansible播放中,我正在重新启动数据库,然后尝试对其进行一些操作.重新启动命令将在重新启动开始后立即返回,而不是在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天全站免登陆