Free tech learning for everyone, from basics to pro

AnsibleProgramming and Application SecurityBeginner

Ansible inventories, groups and variables

In short: The inventory tells Ansible which devices exist, how to reach them and how they are grouped. Variables attached to hosts and groups, kept in host_vars and group_vars folders, let one playbook configure many different devices correctly.

What an inventory does

The inventory is Ansible's list of managed devices. It answers three questions: which hosts exist, how to connect to each one, and which groups they belong to. Groups let you target "all access switches" or "everything at the HQ site" without listing devices by hand. Every host is automatically in the group all.

An inventory grouped by role and siteallevery hostcorecore-sw1, core-sw2accessacc-sw1..sw20firewallsfw-hqsite_hqnested group
Figure 1. Hosts can belong to several groups, such as a role group and a site group, and inherit variables from each.

A YAML inventory

Inventories can be INI or YAML. YAML is clearer once you have nested groups, so most teams use it.

# inventory.yml
all:
  children:
    switches:
      children:
        core:
          hosts:
            core-sw1: { ansible_host: 192.168.10.2 }
            core-sw2: { ansible_host: 192.168.10.3 }
        access:
          hosts:
            acc-sw1:  { ansible_host: 192.168.10.11 }
            acc-sw2:  { ansible_host: 192.168.10.12 }
    site_hq:
      children:
        core:
        access:

Check how Ansible sees it with ansible-inventory -i inventory.yml --graph, and list the variables for one host with ansible-inventory -i inventory.yml --host acc-sw1.

group_vars and host_vars

Rather than cramming variables into the inventory, put them in folders next to it. Ansible loads them automatically by matching file names to group and host names.

inventory.yml
group_vars/
  switches.yml      # connection settings for all switches
  site_hq.yml       # NTP, syslog and DNS servers for HQ
host_vars/
  core-sw1.yml      # loopback, router ID, unique items

# group_vars/switches.yml
ansible_connection: ansible.netcommon.network_cli
ansible_network_os: cisco.ios.ios
ansible_user: netops
ansible_become: true
ansible_become_method: enable

# group_vars/site_hq.yml
ntp_servers: [192.168.10.5, 192.168.10.6]
syslog_server: 192.168.10.20

Passwords do not belong in these files in plain text. Store them encrypted with Ansible Vault, or use SSH keys, and reference them by variable name.

Variable precedence

Variable precedence (simplified)Extra vars-e on command line, winsTask and role varsset in the playhost_varsone devicegroup_vars/<group>a groupgroup_vars/alleverythingRole defaultslowest
Figure 2. When the same variable is set in several places, the more specific source wins; extra vars always win.

Ansible has more than twenty levels of precedence, but the practical rule is simple: specific beats general. A value in host_vars/acc-sw1.yml overrides the same variable in group_vars/switches.yml, which overrides group_vars/all.yml. Values passed with -e on the command line override everything. When a host is in two groups at the same level that set the same variable, the result depends on group ordering, which is confusing; avoid this by giving each variable one clear home.

Inventory typeGood for
Static YAML or INI fileLabs and small, stable networks
Dynamic inventory pluginCloud instances, for example amazon.aws.aws_ec2 or azure.azcollection.azure_rm
Source of truth pluginPulling devices from NetBox with netbox.netbox.nb_inventory

As a network grows, maintaining a static file by hand becomes error-prone. Generating the inventory from a source of truth such as NetBox means devices added there are automatically managed by your playbooks.

Tip: name groups by role (core, access, wan_edge) and by location (site_hq, site_branch1). Most tasks target one of these two dimensions.

Practice

Where would you put the NTP servers used by every device at one site?
In a group_vars file for that site group, for example group_vars/site_hq.yml.
A variable is set in group_vars/all.yml and in host_vars/core-sw1.yml. Which value does core-sw1 use?
The host_vars value, because more specific sources take precedence.
Which command shows the group tree of an inventory?
ansible-inventory -i inventory.yml --graph.

Key takeaways

  • The inventory lists hosts, how to reach them and their groups.
  • Use YAML inventories with nested groups by role and site.
  • Keep variables in group_vars and host_vars folders.
  • More specific variables win; -e extra vars win over everything.
  • Use dynamic inventories for cloud and a source of truth for scale.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.