Reputation: 21
I have an ansible task that is running the "shell" module. I have the task set to retry x amount of times. If the shell command still does not complete successfuly after x amount of retries, how would I make ansible shut down the host?
Here's an example of my shell command with retries:
- name: Check if services are starting
shell: |
isStarting=$(ps -ef | grep -c mysoftware)
if ((isStarting <= 0))
then
sudo systemctl start mysoftware
exit 1
else
exit 0
fi
retries: 5
delay: 10
register: result
until: result.rc == 0
ignore_errors: true
If above task doesnt detect the software starting after 5 retries, I want to shutdown the host.
Upvotes: 1
Views: 581
Reputation: 44645
Using a block with its error handling feature should do the job. Note: I take for granted that the current task is doing the job you expect. Notice I removed ignore_error
since we want to trigger the rescue tasks in this case.
- block
- name: Check if services are starting
shell: |
isStarting=$(ps -ef | grep -c mysoftware)
if ((isStarting <= 0))
then
sudo systemctl start mysoftware
exit 1
else
exit 0
fi
retries: 5
delay: 10
register: result
until: result.rc == 0
rescue:
- name: shutdown server
shell: shutdown -H now
- fail:
msg: "Services did not start, server was stopped"
Upvotes: 1