Include_tasks and loops break parallelism

Situation:

Multiple servers with each one having multiple databases:

inventory file

serverA dbs='["db1","db2"]' 
serverB dbs='["db3","db4","db5"]' 

What I want
to perform task in parallel at host but sequential per DB:

What I tried
main.yml

- name: testcase
  hosts: all
  gather_facts: yes

  tasks:
    - name: "Patching DB"
      include_tasks:
        file: patch_db.yml
      vars:
        - db: '{{ item }}'
      loop: "{{ dbs }}"

patch_db.yml

- name: Patch DB
  debug:
    msg: "patching {{ ansible_host }} and DB {{ db }}"

What I got

PLAY [testcase] ******************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************
ok: [serverB]
ok: [serverA]

TASK [Patching DB] ***************************************************************************************************************************************
included: patch_db.yml for serverA => (item=db1)
included: patch_db.yml for serverA => (item=db2)
included: patch_db.yml for serverB => (item=db3)
included: patch_db.yml for serverB => (item=db4)
included: patch_db.yml for serverB => (item=db5)

TASK [Patch DB] ******************************************************************************************************************************************
ok: [serverA] => {
    "msg": "patching serverA and DB db1"
}

TASK [Patch DB] ******************************************************************************************************************************************
ok: [serverA] => {
    "msg": "patching serverA and DB db2"
}

TASK [Patch DB] ******************************************************************************************************************************************
ok: [serverB] => {
    "msg": "patching serverB and DB db3"
}

TASK [Patch DB] ******************************************************************************************************************************************
ok: [serverB] => {
    "msg": "patching serverB and DB db4"
}

TASK [Patch DB] ******************************************************************************************************************************************
ok: [serverB] => {
    "msg": "patching serverB and DB db5"
}

PLAY RECAP ***********************************************************************************************************************************************
serverA                    : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
serverB                    : ok=7    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Instead of splitting up the hosts and running in parallel it runs sequential

I’m hitting this bug/feature. include_tasks in a loop breaks task execution order · Issue #36978 · ansible/ansible · GitHub

Is there another way to accomplish what I want?

Strategy free does solve this but makes it hard to read the logging.

For roles I’ve written that use loops like this (I didn’t understand and implications or map at the time…) I generally open a terminal / screen session per server, while this is inconvenient it does seem to be one way to speed things up, also each process seems to only use one CPU core so it is a way to make the most of available CPU resources. :person_shrugging:

Yeah but I want to integrate it into awx.

1 Like