Host & Group Variables in the Inventory
Attach connection and config vars to hosts.
Host & Group Variables in the Inventory is a free Ansible Academy lesson on CoddyKit — lesson 4 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.
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=8080Connection 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=2222Override 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.21Group 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=ubuntuVariables 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: 8080Host 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.comKeep 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. 🔧
Frequently asked questions
Is the “Host & Group Variables in the Inventory” lesson free?
Yes — the full text of “Host & Group Variables in the Inventory” 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 “Host & Group Variables in the Inventory”?
Attach connection and config vars to hosts. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
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 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
- INI vs YAML Inventory Files
- Grouping Hosts: web, db & app
- Nested Groups & the children Keyword
- Host & Group Variables in the Inventory