Interview Prep/Ansible/Variables, Facts and Templates

Your play uses `with_items` and you have been asked to modernise. What changes?

BeginnerScenarioVariables, Facts and Templates
Answer

Situation read → Legacy loop syntax.

What I'd do → Replace with_items: "{{ list }}" by loop: "{{ list }}". For nested or flattened lists use filters.

loop: "{{ users | product(groups) | list }}"      # was with_nested
loop: "{{ dict | dict2items }}"                   # was with_dict

Why → loop is the recommended keyword; with_* still works but hides what is happening.

Interview tip → with_items flattens one level automatically; loop does not, so use | flatten(1).

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Templates