Best way to retry task with a list of items until successful

For tasks that depend, for example, on remote servers, it would be very useful to be able to retry the task with a list of items for the remote server names until the task was successful. This would enable defining secondary/fallback servers easily. Is there any elegant way to this with Ansible currently?

Hi Pedro,

You can use the retry/until mechanism to re-execute a task a set number of times, however you cannot change the underlying variables you send to the task during each retry:

http://docs.ansible.com/playbooks_loops.html#do-until-loops

Alternatively, you could use a delegate_to and a when statement to have several tasks that are only run if the previous one failed, so that the second tasks would be delegated to the secondary/tertiary servers if need be. For example:

  • some_module: …
    register: primary_result
    ignore_errors: yes

  • some_module: …
    register: secondary_result
    when: primary_result|failed

ignore_errors: yes

  • some_module: …
    register: tertiary_result
    when: primary_result|failed and secondary_result|failed

Hope that helps!

Thanks James. It does help. I think your second solution is the most appropriate for this case.

–Pedro.