Interview Prep/Ansible/Performance and Scale

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.

All Ansible questions Roadmap: Ansible

More on Performance and Scale