Why is a loop of package installs slower than a list?
BeginnerProductionPerformance and Scale
Answer
A loop runs the module once per item: one connection round trip, one module execution, and one package manager transaction (with dependency solving) per package. Passing a list lets the package manager do a single transaction.
# slow
- ansible.builtin.apt: { name: "{{ item }}" }
loop: [git, curl, jq, htop]
# fast
- ansible.builtin.apt:
name: [git, curl, jq, htop]
state: present
The same applies to user, file and others that accept lists or can be consolidated.
More on Performance and Scale
Why can Ansible be slower than a shell script for 100 servers, and what dominates the time?BeginnerHow do you choose the `forks` value?BeginnerWhat is SSH pipelining and why does it help?BeginnerWhich SSH settings matter for performance and stability?IntermediateHow do you find out which tasks are slow?BeginnerHow do you reduce the cost of fact gathering?Intermediate