check status of async job

I am trying to run async jobs with ‘poll=0’ (fire & forget) and then check the job status via async_status. However, I don’t know how to properly check for job completion.

Here’s my playbook snippet:

  • command: /bin/sleep 10
    async: 25
    poll: 0
    register: sleeper

  • async_status: jid={{ sleeper.ansible_job_id }}
    register: job_result
    until: job_result.finished
    retries: 10
    delay: 1

Of course, the ‘until: job_result.finished’ fails because the ‘finished’ attribute is NOT populated until the job has finished. I have tried doing ‘until: hasattr(job_result,‘finished’)’ as well, but this also fails.

How should I be doing this kind of thing?

Thanks.

If you fire and forget you can’t check status.

You should consider not doing fire-and-forget and setting a poll interval

Thanks, but my experience with adding the poll interval seems to make an async task into a blocking, synchronous task. The playbook does not proceed until the job is finished or the timeout is reached.

What I’d like to do is launch tasks A & B asynchronously so that they execute in parallel. Then I want the playbook to continue with task C only after tasks A & B have both finished.

Is this possible?

Yes, I’m aware that polling does that, it’s intentional.

There’s no way to poll a ‘fire and forget’ job unless you just have it touch a file when you are done.

There’s a do/until loop in 1.4 you could combine with the stat module if you wanted for that purpose.

No, it is not currently possible to use async in this manner. Please feel free to open a feature request for this on github.

Thanks!

Thank you both for the information. Thought maybe I was missing something.

May I suggest a simple code change here, for consideration by the group?

In the async_status module, always return the ‘finished’ attribute (with a 0 or 1 value) instead of doing so ONLY when the job is finished. This would make it trivial to perform the parallelization/waiting that today is not available, using the do…until mentioned in the original post. The code change to async_status would probably be as simple as changing line 84:

module.exit_json(results_file=log_path, ansible_job_id=jid, started=1, finished=0)

More general question: how would I check for the presence of an attribute on a result register?

Yep, open to patches if you’d like to add this and test to make sure it’s still operational/etc.

“More general question: how would I check for the presence of an attribute on a result register?”

Offhand, untested:

when: foo.rc is defined