0Pricing
Ansible Academy · Lesson

Nested Loops with subelements

Iterate lists inside lists.

Nested Loops with subelements is a free Ansible Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Lists Inside Lists

Sometimes each item owns its own list: a user with several SSH keys, a group with many members. That is a nested structure. 🪆

The subelements Lookup

The subelements lookup pairs each parent with each child in its inner list, flattening the nesting into single iterations.

loop: "{{ users | subelements('keys') }}"

Two Arguments

subelements takes the parent list and the name of the inner key to expand. It produces one iteration per child element.

vars:
  users:
    - name: alice
      keys:
        - keyA
        - keyB

item Becomes a Pair

With subelements, item is a two-part list: item.0 is the parent dictionary, and item.1 is the current child value.

- ansible.builtin.debug:
    msg: "{{ item.0.name }} has {{ item.1 }}"
  loop: "{{ users | subelements('keys') }}"

Authorize Many SSH Keys

The textbook case: give each user all their SSH keys. item.0.name is the user, item.1 is one key per iteration.

- ansible.posix.authorized_key:
    user: "{{ item.0.name }}"
    key: "{{ item.1 }}"
  loop: "{{ users | subelements('keys') }}"

Child Can Be a Dict

The inner elements may be dictionaries too. Then item.1 has its own fields, like item.1.path or item.1.mode.

Counting the Iterations

Three users with two keys each means six iterations. subelements multiplies parents by their children automatically.

Skip Missing Inner Lists

If a parent might lack the inner key, add skip_missing so subelements quietly ignores it instead of failing.

loop: "{{ users | subelements('keys', skip_missing=true) }}"

Why Not Two loops?

A task cannot nest two loop keywords directly. subelements is the clean, supported way to walk a parent-child structure in one task.

Readable Beats Clever

Deeply nested data can get hard to follow. Keep your structures shallow when you can; reach for subelements when you genuinely need it.

Still Idempotent

Each pair is a separate module call, so adding an already-present key reports ok. Nesting changes nothing about safe reruns.

Quick Check

Using subelements, what does item.0 refer to?

Recap: Nested Loops

You flattened parent-child data with subelements, used item.0 and item.1, and handled missing lists safely. Next: loop_control. 🎉

Frequently asked questions

Is the “Nested Loops with subelements” lesson free?

Yes — the full text of “Nested Loops with subelements” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.

What will I learn in “Nested Loops with subelements”?

Iterate lists inside lists. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ansible Academy?

No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Nested Loops with subelements” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ansible Academy lesson?

Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The loop Keyword Over a List
  2. Looping Over Dictionaries
  3. Nested Loops with subelements
  4. loop_control: label, index & pause
← Back to Ansible Academy