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?
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'.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With