Any plans for an "await" to go with async stuff?

It seems like it would be really useful to have “await” functionality to go with the async task stuff - instead of poll or “fire-and-forget”, the ability to start async tasks that are going to take a while and receive handles to them which can then be used later in the play to wait for them to succeed or fail to continue.

If this is already possible, I’d appreciate some pointers on how to do it. I didn’t spend a ton of time with the async docs, but I didn’t see anything that seemed to indicate this functionality to me. Did quick searches here, online and on the project mailing list and didn’t see anything either.

I was thinking of something like…

`

We’re working normally (sequentially)…

  • name: Init/update all Git submodules
    command: git submodule update --init

OK, now we’re ready to kick off some tasks that are independent of each other

and can take quite a while to complete:

  • name: Start independent asynchronous background tasks…
    async:

I structured these as a dict to emphasis that they’re not running in

order, like a list might suggest. The keys (user-defined - whatever

you want) could then be used to access results on the handle.

tasks:
git_lfs_pull:
name: Pull Git LFS files
command: git lfs pull

bundle_install:
name: Install gems with Bundler
bundler:
state: present

A handle you receive back that can be used to wait for the tasks.

register: async_handle

Now we can keep working in sequence…

  • name: Install some Python packages with Pip
    with_items:
  • pathlib2
  • docker-compose
    pip:
    name: “{{ item }}”

…whatever else as usual…

OK, now we’re at the point where we need the async tasks to be done

  • name: Wait for async tasks to be done.
    await:
    handle: “{{ async_handle }}”

Once here, we know the async stuff is done and successful (or failed with the

usual message, etc.)

  • name: Kick off a build…
    command: docker-compose build .

`

I think async_status will do what you want already. I’ve not had cause to use it myself but there’s an example about halfway down this page

http://docs.ansible.com/ansible/latest/playbooks_async.html

---
# Requires ansible 1.8+
- name: 'YUM - fire and forget task'
  yum: name=docker-io state=installed
  async: 1000
  poll: 0
  register: yum_sleeper

- name: 'YUM - check on fire and forget task'
  async_status: jid={{ yum_sleeper.ansible_job_id }}
  register: job_result
  until: job_result.finished
  retries: 30

Hope this helps,

Jon

Cool, thanks… I found that example far from intuitive, and it wasn’t clear to me when I looked at it that it facilitated firing off many different tasks at different points, but after loading it up and messing with it for a while I think i have an ugly but functional version of what I was looking for, so I should be able to sugar that up somehow into something like what I want :slight_smile:

Please share it if you can.

By all means create a pull request if you have better examples that are easier to understand. Examples drawn from real-world problems being solved are good to see if they are general enough for others to understand.

All the best,

Jon