DevOps Bootcamp · Lesson

Host & Group Variables in the Inventory

Attach connection and config vars to hosts.

Lesson 4 of 413 steps

Host & Group Variables in the Inventory is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 4 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.

Variables Live With Hosts

Inventories hold more than names. You can attach variables to a host or group so tasks adapt per machine.

Host Variables in INI

In INI, write key=value pairs right after a host on the same line. These apply only to that one host.

web1.example.com http_port=8080

Connection Vars Matter Most

Special vars like ansible_user and ansible_host tell Ansible how to connect, not just what to configure.

db1.example.com ansible_user=deploy ansible_port=2222

Override the Real Address

Use ansible_host to give a friendly inventory name while pointing at a real IP behind the scenes.

app1 ansible_host=10.0.0.21

Group Variables in INI

A [group:vars] section applies one value to every host in that group. Set it once, share it everywhere.

[web:vars]
ansible_user=ubuntu

Variables in YAML Inventory

In YAML, add a vars block to a group, or give a host a mapping of values instead of an empty colon.

web:
  vars:
    ansible_user: ubuntu
  hosts:
    web1.example.com:

Per-Host YAML Vars

Give a single host its own values by nesting them under the host key. The blank colon spot becomes a mapping.

hosts:
  web1.example.com:
    http_port: 8080

Host Beats Group

When a host and its group both set the same variable, the host value wins. Specific always overrides general.

Use the Variable in a Task

Reference an inventory variable with double braces. Here a task reads http_port straight from the host's definition.

- debug:
    msg: "Serving on port {{ http_port }}"

Inspect the Resolved Values

See every variable a host ends up with using the --host flag. Great for debugging which value actually applied.

ansible-inventory -i inventory.ini --host web1.example.com

Keep Inventories Lean

A few inline vars are fine, but heavy config belongs in group_vars files later. Inventory stays about who, not deep how.

Quick Check

A host and its group both define ansible_user. Which value does Ansible use?

Recap

Attach variables to hosts or groups in INI or YAML to set connection and config values. Host beats group on conflict. 🔧

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 “Host & Group Variables in the Inventory” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Host & Group Variables in the Inventory”, 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 “Host & Group Variables in the Inventory”?

Attach connection and config vars to hosts. 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 4 of 4.

How long does the “Host & Group Variables in the Inventory” 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. INI vs YAML Inventory Files
  2. Grouping Hosts: web, db & app
  3. Nested Groups & the children Keyword
  4. Host & Group Variables in the Inventory
← Back to DevOps Bootcamp