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

5,666 views
Skip to first unread message

Pedro Romano

unread,
Nov 20, 2014, 1:16:17 PM11/20/14
to ansible...@googlegroups.com
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?

James Cammarata

unread,
Nov 20, 2014, 2:11:11 PM11/20/14
to ansible...@googlegroups.com
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:


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!

On Thu, Nov 20, 2014 at 12:16 PM, Pedro Romano <pmc...@gmail.com> wrote:
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?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/da9e06af-1480-4fdd-af33-0563c18f2d8b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pedro Romano

unread,
Nov 20, 2014, 2:29:19 PM11/20/14
to ansible...@googlegroups.com
Thanks James. It does help. I think your second solution is the most appropriate for this case.

--Pedro.
Reply all
Reply to author
Forward
0 new messages