DevOps Bootcamp · Lesson

group_vars & host_vars Directories

Organize variables by host and group.

Lesson 2 of 413 steps

group_vars & host_vars Directories is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

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. ✅

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “group_vars & host_vars Directories” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “group_vars & host_vars Directories”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

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

Organize variables by host and group. You practise DevOps Bootcamp 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 DevOps Bootcamp?

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

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 DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp 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 DevOps Bootcamp