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.
More on Jinja2, Conditionals and Loops
What role does Jinja2 play in Ansible?BeginnerWhat are the three Jinja2 delimiters?BeginnerShow a template with a loop and a conditional, and how it is deployed.BeginnerWhich filters do you use every day?BeginnerHow do you transform lists and dicts with filters (`map`, `select`, `selectattr`, `unique`, `flatten`)?IntermediateHow does `when` work and which operators can you use?Beginner