DevOps Bootcamp · Lesson

Nested Loops with subelements

Iterate lists inside lists.

Lesson 3 of 413 steps

Nested Loops with subelements is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

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. 🎉

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Nested Loops with subelements” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Nested Loops with subelements”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

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

Iterate lists inside lists. You practise DevOps Bootcamp 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 DevOps Bootcamp?

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

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 DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp 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 DevOps Bootcamp