What does `group_by` do?

AdvancedConceptInventory
Answer

group_by creates groups at run time from the value of a variable or fact, so later plays can target them.

- hosts: all
  tasks:
    - ansible.builtin.group_by:
        key: "os_{{ ansible_facts['distribution'] | lower }}"
- hosts: os_ubuntu
  tasks:
    - ansible.builtin.apt: { update_cache: true }
- hosts: os_rocky
  tasks:
    - ansible.builtin.dnf: { update_cache: true }

It avoids per-OS when: clauses on every task and keeps each OS family's play simple.

All Ansible questions Roadmap: Ansible

More on Inventory