flyingfish
flyingfish

Reputation: 335

How to run mutiple include tasks asynchronously in ansible?

I use 'include' and 'with_items' to looping over a block of tasks:

---
- name: main file
  gather_facts: false
  hosts: localhost
  vars:
    list1:
      - { name: 'testuser1', groups: 'wheel', time: 10 }
      - { name: 'testuser2', groups: 'root', time: 3 }
  tasks:     
    - name: multiple tasks
      include: multiple.yml item={{item}}
      with_items: "{{ list1 }}"

and the multiple.yml is:

---
- name: time before
  shell: date +"%H:%M:%S"

- name: run 
  shell: sleep {{ item.time }}

- name: time after
  shell: date +"%H:%M:%S"

The task works well. However, the multiple.yml for each loop are executed serialisely:

TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.013440", "end": "2018-11-25 15:56:04.595098", "rc": 0, "start": "2018-11-25 15:56:04.581658", "stderr": "", "stderr_lines": [], "stdout": "15:56:04", "stdout_lines": ["15:56:04"]}

TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 10", "delta": "0:00:10.013043", "end": "2018-11-25 15:56:14.859720", "rc": 0, "start": "2018-11-25 15:56:04.846677", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.012656", "end": "2018-11-25 15:56:15.153993", "rc": 0, "start": "2018-11-25 15:56:15.141337", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}

TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.014217", "end": "2018-11-25 15:56:16.448480", "rc": 0, "start": "2018-11-25 15:56:15.434263", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}

TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 3", "delta": "0:00:03.012509", "end": "2018-11-25 15:56:19.711891", "rc": 0, "start": "2018-11-25 15:56:16.699382", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.013766", "end": "2018-11-25 15:56:20.973979", "rc": 0, "start": "2018-11-25 15:56:19.960213", "stderr": "", "stderr_lines": [], "stdout": "15:56:19", "stdout_lines": ["15:56:19"]}

I want to execute the multiple.yml of each loop asynchronously, so I tried to use async and poll in main file:

---
- name: main file
  gather_facts: false
  hosts: localhost
  vars:
    list1:
      - { name: 'testuser1', groups: 'wheel', time: 10 }
      - { name: 'testuser2', groups: 'root', time: 3 }
  tasks:     
    - name: multiple tasks
      include: multiple.yml item={{item}}
      async: 20  
      poll: 0
      with_items: "{{ list1 }}"

The expected result is:

TASK [time before] ******************************************************************************************
    changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.013440", "end": "2018-11-25 15:56:04.595098", "rc": 0, "start": "2018-11-25 15:56:04.581658", "stderr": "", "stderr_lines": [], "stdout": "15:56:04", "stdout_lines": ["15:56:04"]}

    TASK [run] ******************************************************************************************
    changed: [localhost] => {"changed": true, "cmd": "sleep 10", "delta": "0:00:10.013043", "end": "2018-11-25 15:56:14.859720", "rc": 0, "start": "2018-11-25 15:56:04.846677", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

    TASK [time after] *******************************************************************************************
    changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.012656", "end": "2018-11-25 15:56:15.153993", "rc": 0, "start": "2018-11-25 15:56:15.141337", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}

    TASK [time before] ******************************************************************************************
    changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.014217", "end": "2018-11-25 15:56:16.448480", "rc": 0, "start": "2018-11-25 15:56:15.434263", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:04"]}

    TASK [run] ******************************************************************************************
    changed: [localhost] => {"changed": true, "cmd": "sleep 3", "delta": "0:00:03.012509", "end": "2018-11-25 15:56:19.711891", "rc": 0, "start": "2018-11-25 15:56:16.699382", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

    TASK [time after] *******************************************************************************************
    changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.013766", "end": "2018-11-25 15:56:20.973979", "rc": 0, "start": "2018-11-25 15:56:19.960213", "stderr": "", "stderr_lines": [], "stdout": "15:56:19", "stdout_lines": ["15:56:08"]}

But the result of running new main.yml is the same as before, and the multiple.yml of the two loops is not executed asynchronously. I don't kown what the problem is. Is there any advice?

Upvotes: 6

Views: 5975

Answers (1)

mdaniel
mdaniel

Reputation: 33203

That is a known issue and is marked as an "improvement," rather than a bug. Feel free to drop a comment or +1 or whatever into the issue, but I would not expect it to be resolved in the near future since that issue has been open since 2017.


Separately, it may interest you to know that the "inline variable" syntax has been deprecated in favor of using vars:, but even in that case you don't need to because with_items: automatically exposes item and thus it is implicitly in vars: for you. The deprecation is in the "Note" in the manual

Upvotes: 4

Related Questions