BacklogZero®

Ansible in a Minute · Ansible Core ·

Use loop over a list of hashes

Short answer

Use loop with a list of dictionaries and reference item keys such as item.name in the task.

Subscribe for updates

Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.

By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.

Request

add users from a list of hashes with name and groups keys

Example

A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.

YAML
---
- name: Add several users from a list of hashes
  hosts: all
  become: true
  gather_facts: false
  tasks:
    - name: Add several users
      ansible.builtin.user:
        name: "{{ item.name }}"
        state: present
        groups: "{{ item.groups }}"
      loop:
        - { name: "testuser1", groups: "wheel" }
        - { name: "testuser2", groups: "root" }

How it works

Each loop item is a dictionary. Reference fields with item.key. Prefer this form when each iteration needs more than one value.

When to use it

  • Creating users with names and groups in one task
  • Applying several related fields per list entry

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Not execution-tested
  • Last verified 2026-09-16

Notes

when on a looped task is evaluated for each item. For a plain dictionary of keys, use dict2items or list on the keys instead of a list of hashes.

Reference

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

Get BacklogZero today

Related guides

Back to Ansible in a Minute

Use loop over a list of hashes · Level Up Labs