Retrying a task until a condition is met
You can use theuntilkeyword to retry a task until a certain condition is met. Here’s an example:
This task runs up to 5 times with a delay of 10 seconds between each attempt. If the result of any attempt has “all systems go” in its stdout, the task succeeds. The default value for “retries” is 3 and “delay” is 5.
To see the results of individual retries, run the play with-vv.
When you run a task withuntiland register the result as a variable, the registered variable will include a key called “attempts”, which records the number of retries for the task.
Ifuntilis not specified, the task will retry until the task succeeds but at mostretriestimes (New in version 2.16).
You can combine theuntilkeyword withlooporwith_
. The result of the task for each element of the loop is registered in the variable and can be used in theuntilcondition. Here is an example: