Interview Prep/Ansible/Jinja2, Conditionals and Loops

How do you do a nested loop (all combinations) or loop inside an included file?

AdvancedConceptJinja2, Conditionals and Loops
Answer

Either build the cross product with the product filter, or loop over include_tasks and rename the loop variable in the inner loop.

- ansible.builtin.debug: msg="{{ item.0 }} on {{ item.1 }}"
  loop: "{{ ['web', 'api'] | product([80, 443]) | list }}"
- ansible.builtin.include_tasks: per_user.yml
  loop: "{{ users }}"
  loop_control:
    loop_var: user          # inner file can use its own item

Without loop_var, an inner loop reuses item and Ansible warns about the variable being overwritten.

All Ansible questions Roadmap: Ansible

More on Jinja2, Conditionals and Loops