Interview Prep/Ansible/Inventory and Targeting

A teammate asks for a group that contains all hosts that have more than 16 GB RAM.

AdvancedScenarioInventory and Targeting
Answer

Situation read → Group membership depends on facts, which exist only at run time.

What I'd do → Use group_by after fact gathering.

- hosts: all
  tasks:
    - ansible.builtin.group_by:
        key: "bigmem_{{ ansible_memtotal_mb > 16384 }}"

- hosts: bigmem_True
  tasks:
    - ansible.builtin.debug: { msg: "large host" }

Why → group_by builds groups in memory for the rest of the run; inventory files cannot know facts in advance.

Interview tip → Alternatively use the constructed inventory plugin with cached facts for a persistent version.

All Ansible questions Roadmap: Ansible

More on Inventory and Targeting