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

**URL:** <https://forum.ansible.com/t/best-way-to-retry-task-with-a-list-of-items-until-successful/16835>\
**Category:** Ansible Project\
**Created:** [November 20, 2014, 6:16pm UTC](https://forum.ansible.com/t/best-way-to-retry-task-with-a-list-of-items-until-successful/16835 "2014-11-20T18:16:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pedro\_Romano](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@Pedro\_Romano](https://forum.ansible.com/u/Pedro_Romano)\
**Post date:** [November 20, 2014, 6:16pm UTC](https://forum.ansible.com/t/best-way-to-retry-task-with-a-list-of-items-until-successful/16835/1 "2014-11-20T18:16:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [November 20, 2014, 7:10pm UTC](https://forum.ansible.com/t/best-way-to-retry-task-with-a-list-of-items-until-successful/16835/2 "2014-11-20T19:10:59Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![Pedro\_Romano](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@Pedro\_Romano](https://forum.ansible.com/u/Pedro_Romano)\
**Post date:** [November 20, 2014, 7:29pm UTC](https://forum.ansible.com/t/best-way-to-retry-task-with-a-list-of-items-until-successful/16835/3 "2014-11-20T19:29:19Z")

</div>

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

–Pedro.
