How to retry Ansible task that may fail?


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.

When I do retries as follows

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?


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

The following task:


which seems to be exactly what you want.

Leave a Reply