Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retry Ansible task that may fail?

Tags:

ansible

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

retries: 3 delay: 5 

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?

like image 814
Bartosz Bilicki Avatar asked May 23 '17 12:05

Bartosz Bilicki


People also ask

How do you handle failure in Ansible?

By default Ansible stops executing tasks on a host when a task fails on that host. You can use ignore_errors to continue on in spite of the failure. The ignore_errors directive only works when the task is able to run and returns a value of 'failed'.

What happens when one playbook fails a task?

The default workflow is to fail, then ignore that host for the rest of the playbook. Then at the end, admin researches why the hosts failed, fixes them, then reruns the playbook against the ones that failed. The . retry file option helps with this.

What is Ansible retry file?

retry file will contain the hostname of the managed hosts (e.g. target system). cat foo.yml server1.example.com. This occurs because, by default, the /etc/ansible/ansible. cfg has the following. retry_files_enabled = True.


1 Answers

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:

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

produces:

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": []} 

which seems to be exactly what you want.

like image 200
techraf Avatar answered Sep 22 '22 05:09

techraf