AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible group_vars & host_vars: Organize Variables by Host and Group

By Luca Berton · Published 2024-01-01 · Category: linux-administration

Complete guide to Ansible group_vars and host_vars directories. Organize variables by group and host, use vault-encrypted files, understand variable precedence, and structure multi-environment inventories with practical examples.

group_vars and host_vars are directories where you store variables for groups and individual hosts. They keep your inventory clean and your variables organized by environment, role, or host.

Directory Structure

group_vars: Variables by Group

host_vars: Variables by Host

Using Variables in Playbooks

Directory Format (Multiple Files)

Split variables into multiple files per group:

Multi-Environment Setup

Variable Precedence

From lowest to highest priority: group_vars/all (lowest) group_vars/ host_vars/ Play vars: Task vars: set_fact / register -e / --extra-vars (highest)

hostvars: Access Other Host's Variables

Vault-Encrypted Variables

Best practice — reference vault variables with a vault_ prefix:

Practical Examples

Complete Project Structure

Dynamic Variables per Environment

FAQ

Where should I put group_vars — next to inventory or playbook?

Both locations work. Ansible checks group_vars/ next to the inventory file AND next to the playbook. For multi-environment setups, keep them with the inventory. For single-inventory projects, put them at the project root.

Can I use subdirectories inside group_vars?

Yes. group_vars/webservers/ (a directory) works the same as group_vars/webservers.yml (a file). All YAML files in the directory are loaded and merged. Use directories when you want to separate vault files from plain vars.

How do I set variables for a child group?

If webservers is a child of linux, variables in group_vars/linux.yml apply to webservers too. Child group vars override parent group vars.

What's the difference between hostvars and host_vars?

host_vars/ is a directory for storing per-host variable files. hostvars is a runtime variable in Ansible that lets you access any host's variables during playbook execution: hostvars['hostname'].variable_name.

Conclusion

Use group_vars/all.yml for shared defaults, group_vars/.yml for group-specific settings, and host_vars/.yml for per-host overrides. Split sensitive data into vault-encrypted files using the directory format. This keeps your inventory clean and your secrets separate from plain configuration.

Related ArticlesAnsible Inventory GuideAnsible Vault Deep DiveAnsible set_fact vs vars vs extra_varsAnsible gather_facts Guide

Category: linux-administration

Browse all Ansible tutorials · AnsiblePilot Home