Interview Prep/Ansible/Vault, Performance and Execution Behaviour

A role that runs `include_tasks` in a loop over 3,000 firewall rules takes 40 minutes

AdvancedTroubleshootingVault, Performance and Execution Behaviour
Answer

Likely causes → Each loop iteration of include_tasks is processed per host: templating, a task object for each item, and a separate module run with its own SSH round trip. With thousands of items, the overhead dominates.

Fix → Push the loop down to the target:

  • Template a full ruleset file (nftables.conf, iptables-restore format) once, and apply it with a handler.
  • Use modules that accept lists, such as package with a list of names.
  • If you must loop, loop over a module directly instead of over include_tasks.

Prevent → Profile with profile_tasks, and treat any task with more than a few hundred iterations as a sign the design needs to change.

All Ansible questions Roadmap: Ansible

More on Vault, Performance and Execution Behaviour