I have an executable, lets say /tmp/foo
I want to try to run (5 times at most) this binary file until I see a line in a log file: lets say /tmp/start.log
I need such kind of a play:
- block:
    - shell: /tmp/foo
    - pause: seconds=30
    - name: Check if started successfully
      shell: grep 'started successfully' /tmp/start.log
      register: grep
  retry: 5
  until: grep.stdout 
But unfortunately Ansible block does not support retry-until.
How can I achieve this?
I'd shot with single command:
- shell: /tmp/foo; sleep 30; grep 'started successfully' /tmp/start.log
  register: cmd_result
  retries: 5
  until: cmd_result | success
For this case using a plain shell script should be the easiest method.
Shell script start_foo.sh (depending on the /tmp/foo exit code you might control if and where the script should fail with set -e and set +e):
#!/bin/sh
set -e    # fail the script if the command fails
/tmp/foo
sleep 30
set +e    # do not fail the script when string is not found
grep 'started successfully' /tmp/start.log
exit 0
Ansible task:
- script: start_foo.sh
  register: grep
  retry: 5
  until: grep.stdout 
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With