Ansible task async with poll=0 doesn't move on from task

Viewed 177

ansible version: 4.10.0

I am hoping to make this task async on a single host. When I use poll:0, I had expected it to fire off the task and move on, but it instead takes longer than when async is not used.

- name: "setup test databases on cluster"
  block:
    - name: "generate list of databases"
      set_fact:
        test_database_list: "{{ test_database_list | default([])+['test_db%02d'| format(item)] }}"
      loop: "{{ range(1,10+1,1)| list }}"
    - name: "create test database(s) on cluster"
      postgresql_db:
        name: "{{ item }}"
      with_items: "{{ test_database_list }}"
  become_user: postgres
- name: "install postgresql extensions"
  postgresql_ext:
    db: "{{ item[0] }}"
    name: "{{ item[1] }}"
  with_nested:
    - "{{ test_database_list }}"
    - - pageinspect
      - pg_buffercache
      - pg_freespacemap
      - pg_prewarm
      - pg_stat_statements
      - pg_visibility
      - pg_trgm
      - pgrowlocks
      - pgstattuple
  become_user: postgres
  async: 30
  poll: 0
  register: postgres_ext_install_status

- ansible.builtin.async_status:
    jid: "{{ item.ansible_job_id }}"
  loop: "{{ postgres_ext_install_status.results }}"
  loop_control:
    label: "{{ item.item }}"
  register: job_result
  until: job_result.finished
  retries: 100
  delay: 2
- debug:
    msg: "{{ item.start }} -- {{ item.end }} -- {{ item.delta }}"
  loop: "job_result.results"
  loop_control:
    label: "{{ item.item.item }}

Here is the output: https://pastebin.com/DTkbbqgR

The documentation clearly states that poll:0 will cause the play to move on from the task, but it instead stays on the task and slowly moves through each of the extensions for each database before moving onto the next task.

Am I misunderstanding the use of async / poll?

The following example works for running async on a single host and having it move on from the task.

- hosts: localhost
  gather_facts: no
  tasks:
    - name: sleep 10s
      command: "sleep 10"
      loop: "{{range(3)|list}}"
      async: 11
      poll: 0

How is this different from what I am doing?

EDIT: Added more code and link to results

1 Answers

Use aync_status and take a look at the timing, e.g.

- hosts: localhost
  gather_facts: false
  tasks:
    - shell: 'sleep 2 && echo {{ item.0 }}{{ item.1 }}'
      with_nested:
        - [a, b]
        - [1, 2, 3]
      async: 30
      poll: 0
      register: result

    - async_status:
        jid: "{{ item.ansible_job_id }}"
      loop: "{{ result.results }}"
      loop_control:
        label: "{{ item.item }}"
      register: job_result
      until: job_result.finished
      retries: 100
      delay: 2
    - debug:
        msg: "{{ item.start }} {{ item.end }} {{ item.delta }}"
      loop: "{{ job_result.results }}"
      loop_control:
        label: "{{ item.item.item }}"

gives

PLAY [localhost] ************************************************************

TASK [shell] ****************************************************************
changed: [localhost] => (item=['a', 1])
changed: [localhost] => (item=['a', 2])
changed: [localhost] => (item=['a', 3])
changed: [localhost] => (item=['b', 1])
changed: [localhost] => (item=['b', 2])
changed: [localhost] => (item=['b', 3])

TASK [async_status] *********************************************************
FAILED - RETRYING: [localhost]: async_status (100 retries left).
changed: [localhost] => (item=['a', 1])
changed: [localhost] => (item=['a', 2])
changed: [localhost] => (item=['a', 3])
changed: [localhost] => (item=['b', 1])
changed: [localhost] => (item=['b', 2])
changed: [localhost] => (item=['b', 3])

TASK [debug] ****************************************************************
ok: [localhost] => (item=['a', 1]) => 
  msg: 2022-02-23 01:43:53.697183 2022-02-23 01:43:55.703037 0:00:02.005854
ok: [localhost] => (item=['a', 2]) => 
  msg: 2022-02-23 01:43:53.987039 2022-02-23 01:43:55.993030 0:00:02.005991
ok: [localhost] => (item=['a', 3]) => 
  msg: 2022-02-23 01:43:54.269019 2022-02-23 01:43:56.275290 0:00:02.006271
ok: [localhost] => (item=['b', 1]) => 
  msg: 2022-02-23 01:43:54.592526 2022-02-23 01:43:56.601432 0:00:02.008906
ok: [localhost] => (item=['b', 2]) => 
  msg: 2022-02-23 01:43:54.880133 2022-02-23 01:43:56.886689 0:00:02.006556
ok: [localhost] => (item=['b', 3]) => 
  msg: 2022-02-23 01:43:55.220324 2022-02-23 01:43:57.228616 0:00:02.008292
Related