ansible until with number of retries

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

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
        Racke

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

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

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