0Pricing
Ansible Academy · Lesson

group_vars & host_vars Directories

Organize variables by host and group.

group_vars & host_vars Directories is a free Ansible Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Stop Hardcoding Vars

Burying values inside playbooks gets messy fast. Ansible auto-loads variables from group_vars and host_vars directories instead. 🗂️

How group_vars Loads

A file in group_vars named after a group is applied to every host in that group, with no manual include needed.

# group_vars/webservers.yml
http_port: 80
worker_processes: 4

The all Group

A special file group_vars/all.yml sets defaults for every host in your inventory, perfect for org-wide settings.

# group_vars/all.yml
timezone: UTC
ntp_server: pool.ntp.org

How host_vars Loads

Files in host_vars are named after a single host and apply only to that machine, ideal for per-server overrides.

# host_vars/web01.example.com.yml
server_id: 1
backup_disk: /dev/sdb

Name Must Match

The filename must match the group or host name exactly as it appears in inventory, or Ansible silently skips loading it.

Files or Directories

Each entry can be a single .yml file or a directory of many files. Ansible merges every file inside a directory together.

group_vars/
  webservers/
    network.yml
    tuning.yml

Host Beats Group

When the same variable appears in both, host_vars wins over group_vars. More specific scope always overrides the broader one.

Child Group Precedence

Among groups, a child group overrides its parent, so nested groups give you layered, predictable defaults.

Relative to Inventory

Ansible looks for these directories next to your inventory file first, and also next to the playbook being run.

Reference Them Normally

Inside tasks you use these variables exactly like any other, with the {{ }} syntax. The source is invisible to the task.

- name: Open the web port
  debug:
    msg: "Port is {{ http_port }}"

Cleaner Playbooks

With data externalized, your plays describe what to do while group_vars and host_vars hold the per-environment numbers. 🎯

Quick Check

A variable is set in both group_vars and host_vars. Which value wins?

Recap

You learned how group_vars and host_vars auto-load by name, that host beats group, and child beats parent. Externalize data, keep plays clean. ✅

Frequently asked questions

Is the “group_vars & host_vars Directories” lesson free?

Yes — the full text of “group_vars & host_vars Directories” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.

What will I learn in “group_vars & host_vars Directories”?

Organize variables by host and group. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ansible Academy?

No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “group_vars & host_vars Directories” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ansible Academy lesson?

Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Recommended Directory Layout
  2. group_vars & host_vars Directories
  3. Splitting Playbooks with import & include
  4. Naming, Comments & Style That Scale
← Back to Ansible Academy