Interview Prep/Ansible/Playbook Design, Handlers and Idempotency

You want to loop over a list of users and create each with its own groups.

BeginnerScenarioPlaybook Design, Handlers and Idempotency
Answer

Situation read → A data-driven loop.

What I'd do →

vars:
  team:
    - { name: asha, groups: [sudo, docker] }
    - { name: ravi, groups: [docker] }
tasks:
  - ansible.builtin.user:
      name: "{{ item.name }}"
      groups: "{{ item.groups }}"
      append: true
    loop: "{{ team }}"
    loop_control:
      label: "{{ item.name }}"

Why → loop_control.label keeps output short and avoids leaking big dicts into logs.

Interview tip → append: true is vital, or you remove existing group memberships.

All Ansible questions Roadmap: Ansible

More on Playbook Design, Handlers and Idempotency