仅当任务 1 通过异步轮询超时时才执行任务 2

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

仅当任务 1 未在异步时间内完成时才执行任务 2。

基本上,如果我想在 catalina.out.

的 120 秒内未收到字符串 "Server Startup" 字符串时发送 catalina.out 日志文件

但是任务 2 每次都在执行,我希望它仅在异步任务失败时执行。

任务 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

任务 2:

handlers:
    - name: send mail
      mail:
        to: mayank.arora@test.com
        subject: |
               {body code}

处理程序是为不同的目的而设计的。

您需要做的就是注册第一个任务的 return 值和第二个 运行 只有当第一个任务失败时的值:

- 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: mayank.arora@test.com
    subject: |
           {body code}
  when: my_task.failed