2022-12-16

Ansible - Retrying 2 tasks of a role

I am implementing a role in ansible where I need to:

  1. Start an application (retry max of 3 times). If the app was successfully started, it will return "OK", otherwise it will return "NOK".
  2. If the app was not successfully started (NOK), we should try to delete a xpto directory (max 3 times - after each NOK status). After this 3 times, (if the app was not successfully started) we should get fail status, and we should abort execution.
  3. If the app starts OK, there is no need to clean the directory, and we are ready to run the app.

We need to be aware of this:

  • Each time I try to start the app, if I get status "NOK" I must run a task to delete the xpto directory.
  • We can retry up to 3 times to start the app (and to delete the directory).
  • Each time we try to start the app with NOK status we must run the task to delete the directory.
  • If at any attempt of starting the app we get status OK (app started with success), we don't want to run task to delete the directory - In this case we should move to last task to run the app.
  • The role has only this 3 tasks (start app, delete directory, run the app)

For now I have only this with where I am missing a lot of the mentioned features:

---    
- name: Start app
  command: start app
  register: result
  tags: myrole

- name: Delete directory xpto if app didn't started ok
  command: rm -rf xpto
  when:
    - result.stdout is search("NOK")
  tags: myrole

- name: Run the application
  command: app run xpto
  when:
    - result.stdout is search("OK")
  tags: myrole

I have been pointed to an other question with a response which allows me to implement the 3 retries with abort option.

I am still missing the way to implement the option if the app starts ok (task1) and proceed directly to run the app (task3) (not going throw task2) and I don't know where to start.



No comments:

Post a Comment