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.
More on Inventory
What is an inventory and why does Ansible need one?BeginnerWhat are the INI and YAML inventory formats and how do they differ?BeginnerHow do you create groups of groups?BeginnerExplain host patterns: `:`, `:&`, `:!` and wildcards.IntermediateWhich connection variables can you set per host in inventory?BeginnerWhat are `group_vars` and `host_vars` directories?Intermediate