mayank arora
mayank arora

Reputation: 117

Execute task 2 only if task 1 times out via async poll

Want to execute tasks 2 only if task 1 does not complete in async time.

Basically if I want to mail catalina.out log file when i dont get the string "Server Startup" string in 120 seconds in catalina.out.

But task 2 is getting executed every time, I want it to be executed only when async fails the task.

Task 1 :

- name: Check startup status in logs
    shell: sh -c 'tail -f  --pid=$$ /opt/data/tomcat/{{ lookup('file', '/etc/ansible/scripts/deployment/check.ini') }}/logs/catalina.out | { grep -i -m 1 "Server startup in" && kill $$ || true ;}' || exit 0 ; 
    ignore_errors: yes
    async: 120
    poll: 10
    notify:
    - send mail

Task 2:

handlers:
    - name: send mail
      mail:
        to: [email protected]
        subject: |
               {body code}

Upvotes: 0

Views: 457

Answers (1)

techraf
techraf

Reputation: 68559

Handlers were designed for a different purpose.

All you need to do is register the return values of the first task and run the second only if the task first one failed:

- name: Check startup status in logs
  shell: sh -c 'tail -f  --pid=$$ /opt/data/tomcat/{{ lookup('file', '/etc/ansible/scripts/deployment/check.ini') }}/logs/catalina.out | { grep -i -m 1 "Server startup in" && kill $$ || true ;}' || exit 0 ; 
  ignore_errors: yes
  async: 120
  poll: 10
  register: my_task

- name: send mail
  mail:
    to: [email protected]
    subject: |
           {body code}
  when: my_task.failed

Upvotes: 1

Related Questions