Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit ansible playbook task concurrency

I'm updating several hosts with ansible at the same time, however I have a limitation...

I have to download artifacts from a common repository with no more than 3 simultaneous downloads!

The current solution I have is to limit the whole playbook to max three concurrent tasks

strategy: linear
serial: 3

Is it possible to limit concurrency only for particular task step rather than the whole playbook?

like image 930
guillem Avatar asked Feb 06 '18 12:02

guillem


People also ask

How do you handle long running tasks in Ansible?

For long running asynchronous tasks, it's good to set poll=0 so that Ansible can immediately jump to the next task after starting the current one without waiting for the result. Register: It is basically used to register the results on a task in a variable.

How do I make an Ansible task run only once?

The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the “ansible-playbook” command. The default behavior is to execute all the tags in your Playbook with --tags all .

Does Ansible run tasks in parallel?

Interact with multiple hosts simultaneously, on a per-playbook basis with Ansible's serial keyword. Parallelism describes a software's ability to spawn multiple processes to execute tasks in tandem. It also applies to Ansible's default ability to interact with numerous hosts simultaneously.


2 Answers

There's no direct way. Only workarounds like run_once loop with delegate_to or multiplying the task with loop and executing only one item per host.

See issue #12170, which is closed with "won't fix" status for details.

delegate_to loop:

- mytask: ..
  delegate_to: "{{item}}"
  run_once: true
  # many diff ways to make the loop
  with_inventory_hostnames: all

multiplied task:

- name: target task
  debug: msg="Performing task on {{ inventory_hostname }}, item is {{ item }}"
  with_items: "{{ play_hosts }}"
  when: "hostvars[item].inventory_hostname == inventory_hostname"
like image 152
Konstantin Suvorov Avatar answered Sep 23 '22 17:09

Konstantin Suvorov


Yes, it's possible to only limit the concurrency of a certain task.

You just need to add the throttle keyword to your download task.

Example:

- name: Download payload.tar.gz
  get_url:
    url: https://example.com/path/payload.tar.gz
    dest: /mnt/scratch/payload.tar.gz
    mode: '0640'
  throttle: 3

Note that throttle was introduced in Ansible 2.9.

like image 42
maxschlepzig Avatar answered Sep 22 '22 17:09

maxschlepzig