Interview Prep/Ansible/Playbooks, Plays, Tasks and Handlers

What does the registered variable look like when a task uses `loop`?

AdvancedConceptPlaybooks, Plays, Tasks and Handlers
Answer

The registered variable has a results list with one entry per item, each carrying the module's output plus item. The top-level changed is true if any item changed, and failed true if any failed.

- ansible.builtin.command: "id {{ item }}"
  loop: [alice, bob, ghost]
  register: ids
  failed_when: false

- ansible.builtin.debug:
    msg: "{{ ids.results | selectattr('rc', 'ne', 0) | map(attribute='item') | list }}"

The example prints the users for whom id failed. Use loop_control: label: "{{ item.name }}" to keep output readable with large dicts.

All Ansible questions Roadmap: Ansible

More on Playbooks, Plays, Tasks and Handlers