5

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?

turkenh
  • 2,564
  • 3
  • 25
  • 32

2 Answers2

8

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
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
3

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 
techraf
  • 64,883
  • 27
  • 193
  • 198