# ansible until with number of retries

**URL:** <https://forum.ansible.com/t/ansible-until-with-number-of-retries/32809>\
**Category:** Ansible Project\
**Created:** [April 29, 2020, 8:52am UTC](https://forum.ansible.com/t/ansible-until-with-number-of-retries/32809 "2020-04-29T08:52:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivo\_Hechmann](https://avatars.discourse-cdn.com/v4/letter/i/91b2a8/32.png) [@Ivo\_Hechmann](https://forum.ansible.com/u/Ivo_Hechmann)\
**Post date:** [April 29, 2020, 8:52am UTC](https://forum.ansible.com/t/ansible-until-with-number-of-retries/32809/1 "2020-04-29T08:52:47Z")

</div>

Hello team,

i want to call a shell script from ansible, this script may fail, so ansible should retry the command, lets say 5 tries:

- name: process something, fail if not 5 files in tmpdir  
shell: “/home/ivo/workspace/splunk-envs/scripts/retry/run.sh”

ignore\_errors: True  
register: status\_var  
until: status\_var is not failed  
delay: 1  
retries: 4

That works pretty fine. Now, how can I pass the retry number to the run.sh? The script should log, and it would be nice if the script would know if there will be a retry on failure or not (last run or so).  
I saw in documentation that there is some loop\_control and ansible\_loop\_index, but i cannot combine these variables with the shell command in an until-loop. Is there a syntax problem or some better  
way the achieve my goal?

What I have tried:

- name: process something, fail if not 5 files in tmpdir  
loop\_control:  
extended: yes  
shell: “/home/ivo/workspace/splunk-envs/scripts/retry/run.sh {{ lookup(‘vars’, ansible\_loop.index) }}”

ignore\_errors: True  
register: status\_var  
until: status\_var is not failed  
delay: 1  
retries: 4

Thank you very much for any hints on this…  
Ivo

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 29, 2020, 10:16am UTC](https://forum.ansible.com/t/ansible-until-with-number-of-retries/32809/2 "2020-04-29T10:16:56Z")

</div>

> Hello team,
> 
> i want to call a shell script from ansible, this script may fail, so ansible should retry the command, lets say 5 tries:
> 
> - name: process something, fail if not 5 files in tmpdir  
> shell: "/home/ivo/workspace/splunk-envs/scripts/retry/run.sh"  
> ignore\_errors: True  
> register: status\_var  
> until: status\_var is not failed  
> delay: 1  
> retries: 4
> 
> That works pretty fine. Now, how can I pass the retry number to the run.sh? The script should log, and it would be nice  
> if the script would know if there will be a retry on failure or not (last run or so).  
> I saw in documentation that there is some loop\_control and ansible\_loop\_index, but i cannot combine these variables with  
> the shell command in an until-loop. Is there a syntax problem or some better  
> way the achieve my goal?
> 
> What I have tried:
> 
> - name: process something, fail if not 5 files in tmpdir  
> loop\_control:  
> extended: yes  
> shell: "/home/ivo/workspace/splunk-envs/scripts/retry/run.sh {{ lookup('vars', ansible\_loop.index) }}"  
> ignore\_errors: True  
> register: status\_var  
> until: status\_var is not failed  
> delay: 1  
> retries: 4
> 
> Thank you very much for any hints on this...  
> Ivo

I don't understand why Ansible should handle the retries. Write a proper script or a wrapper around the existing one.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 29, 2020, 10:30am UTC](https://forum.ansible.com/t/ansible-until-with-number-of-retries/32809/3 "2020-04-29T10:30:45Z")

</div>

IMHO, neither "shell" nor "loop\_control" will make it. The runstring in  
"shell" will be evaluated only once when the module starts. The  
"loop\_control" looks promising, but there is no option how to break a "loop"  
in Ansible.

The best option would be to pass a variable, for example "number\_of  
retries" both to the script and to the module, and let the script make the  
counting on its own. For example,

&nbsp;&nbsp;- name: process something, fail if not 5 files in tmpdir  
&nbsp;&nbsp;&nbsp;&nbsp;command: "run.sh {{ number\_of\_retries }}"  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;retries: "{{ number\_of)retries }}"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Ivo\_Hechmann](https://avatars.discourse-cdn.com/v4/letter/i/91b2a8/32.png) [@Ivo\_Hechmann](https://forum.ansible.com/u/Ivo_Hechmann)\
**Post date:** [April 29, 2020, 12:21pm UTC](https://forum.ansible.com/t/ansible-until-with-number-of-retries/32809/4 "2020-04-29T12:21:02Z")

</div>

Hello Stefan and Vladimir…

Yeah I think you are right, handling that in ansible would prevent me to put that retry-poison in shell, but it looks like there is no suitable solution in ansible, so I will put a shell wrapper in front of the scripts.  
Thank you very much for your thoughts…  
Ivo

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 29, 2020, 1:08pm UTC](https://forum.ansible.com/t/ansible-until-with-number-of-retries/32809/5 "2020-04-29T13:08:42Z")

</div>

Well, this is how Ansible works. A task is sent from the controller to the  
remote host and the remote host will return the results to the controller  
after the task will have completed.

When the time-out of the remote host in not predictable "Asynchronous Actions  
and Polling" might help to improve the efficiency  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_async.html#asynchronous-actions-and-polling](https://docs.ansible.com/ansible/latest/user_guide/playbooks_async.html#asynchronous-actions-and-polling)

In some case "ansible-pull" might fit the use-case better.  
[https://docs.ansible.com/ansible/latest/cli/ansible-pull.html#ansible-pull](https://docs.ansible.com/ansible/latest/cli/ansible-pull.html#ansible-pull)

HTH,

&nbsp;&nbsp;-vlado
