Ansible keyword - throttle

The default behavior of ansible-playbook is the linear strategy:

Task execution is in lockstep per host batch as defined by serial (default all). Up to the fork limit of hosts will execute each task at the same time and then the next series of hosts until the batch is done, before going on to the next task.

throttle serves the purpose of "limiting the number of workers for a particular task ... Use throttle to restrict tasks that may be CPU-intensive or interact with a rate-limiting API".

For example, by default execute the task in parallel at all (limited by serial up to the forks) hosts

- hosts: host1,host2,host3
  tasks:
    - command: sleep 1
      register: result
    - debug:
        msg: "{{ inventory_hostname }} task1
              {{ result.start }} - {{ result.end }}"

You can see that all hosts executed the task in parallel

ok: [host2] => 
  msg: host2 task1 2021-07-23 08:56:14.735263 - 2021-07-23 08:56:15.759474
ok: [host1] => 
  msg: host1 task1 2021-07-23 08:56:14.743419 - 2021-07-23 08:56:15.759518
ok: [host3] => 
  msg: host3 task1 2021-07-23 08:56:14.475249 - 2021-07-23 08:56:15.551159

If you limit the execution by throttle

    - command: sleep 1
      register: result
      throttle: 1
    - debug:
        msg: "{{ inventory_hostname }} task2
              {{ result.start }} - {{ result.end }}"

You can see that the execution of the task was serialized

ok: [host2] => 
  msg: host2 task2 2021-07-23 08:56:20.252623 - 2021-07-23 08:56:21.312210
ok: [host3] => 
  msg: host3 task2 2021-07-23 08:56:22.688410 - 2021-07-23 08:56:23.744130
ok: [host1] => 
  msg: host1 task2 2021-07-23 08:56:17.937011 - 2021-07-23 08:56:18.955631

Make sure the time has been synchronized before you start experimenting.


Q: "Using include_tasks I am not getting the same behavior. All the included tasks are running in parallel."

A: I can't reproduce your problem. Given the files

shell> ansible --version
ansible 2.10.1

shell> cat test-119-tasks-2.yml
---
- command: sleep 1
  register: result
- debug:
    msg: "{{ inventory_hostname }} task2
          {{ result.start }} - {{ result.end }}"

shell> cat test-119-tasks-3.yml
---
- command: sleep 1
  register: result
- debug:
    msg: "{{ inventory_hostname }} task3
          {{ result.start }} - {{ result.end }}"

the task

    - name: Execute sequentially
      throttle: 1
      block:
        - include_tasks: test-119-tasks-2.yml
        - include_tasks: test-119-tasks-3.yml

gives

TASK [include_tasks] ***********************************************
included: /export/scratch/tmp8/test-119-tasks-2.yml for host1, host2, host3
TASK [command] *****************************************************
changed: [host1]
changed: [host2]
changed: [host3]

TASK [debug] *******************************************************
ok: [host1] => 
  msg: host1 task2 2021-07-25 10:56:11.832219 - 2021-07-25 10:56:12.848834
ok: [host2] => 
  msg: host2 task2 2021-07-25 10:56:14.312498 - 2021-07-25 10:56:15.330202
ok: [host3] => 
  msg: host3 task2 2021-07-25 10:56:16.751018 - 2021-07-25 10:56:17.774723

TASK [include_tasks] ***********************************************
included: /export/scratch/tmp8/test-119-tasks-3.yml for host1, host2, host3

TASK [command] ******************************************************
changed: [host1]
changed: [host2]
changed: [host3]

TASK [debug] ********************************************************
ok: [host1] => 
  msg: host1 task3 2021-07-25 10:56:20.373052 - 2021-07-25 10:56:21.405847
ok: [host2] => 
  msg: host2 task3 2021-07-25 10:56:22.761254 - 2021-07-25 10:56:23.788941
ok: [host3] => 
  msg: host3 task3 2021-07-25 10:56:25.134650 - 2021-07-25 10:56:26.151786

You can see that the execution of the included tasks was serialized. This works as expected because the keyword throttle is allowed in the block. The application of such keyword to a block is the same as the application of this keyword to all tasks in the block.