ansible until with number of retries

43 views
Skip to first unread message

Ivo Hechmann

unread,
Apr 29, 2020, 4:52:47 AM4/29/20
to Ansible Project
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 

Stefan Hornburg (Racke)

unread,
Apr 29, 2020, 6:17:24 AM4/29/20
to ansible...@googlegroups.com
I don't understand why Ansible should handle the retries. Write a proper script or a wrapper around the existing one.

Regards
Racke

>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/f489599e-f1fc-4f32-b64e-7aa77293e6b2%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/f489599e-f1fc-4f32-b64e-7aa77293e6b2%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Vladimir Botka

unread,
Apr 29, 2020, 6:31:04 AM4/29/20
to Ivo Hechmann, ansible...@googlegroups.com
On Wed, 29 Apr 2020 01:52:47 -0700 (PDT)
Ivo Hechmann <ivo.he...@gmail.com> wrote:

> 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
>
> ... how can I pass the retry number to the run.sh?
> ... 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?

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,

- name: process something, fail if not 5 files in tmpdir
command: "run.sh {{ number_of_retries }}"
...
retries: "{{ number_of)retries }}"

HTH,

-vlado

Ivo Hechmann

unread,
Apr 29, 2020, 8:21:02 AM4/29/20
to Ansible Project
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

Vladimir Botka

unread,
Apr 29, 2020, 9:09:02 AM4/29/20
to Ivo Hechmann, ansible...@googlegroups.com
On Wed, 29 Apr 2020 05:21:02 -0700 (PDT)
Ivo Hechmann <ivo.he...@gmail.com> wrote:

> ... it looks like there is no suitable solution in ansible

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

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

HTH,

-vlado
Reply all
Reply to author
Forward
0 new messages