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
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.
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 }}"
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.