Stage 4 · Provision
Playbooks & Variables
Variables & Precedence
group_vars, host_vars, extra-vars, and the 22-level precedence rules.
Variable Sources
Ansible variables can come from many places — inventory files, playbook vars, role defaults, extra-vars, facts, and more. Understanding where variables come from and which takes precedence is critical for predictable automation.
---
- name: Variables example
hosts: webservers
vars:
http_port: 80
app_name: myapp
tasks:
- name: Show variables
debug:
msg: "Running {{ app_name }} on port {{ http_port }}"The vars section defines variables at the play level. These are available to all tasks in the play.
group_vars and host_vars
The recommended way to organize variables is using group_vars and host_vars directories. These directories sit alongside your inventory file and are automatically loaded.
inventory/
hosts.yml
group_vars/
all.yml # Variables for all hosts
webservers.yml # Variables for webservers group
dbservers.yml # Variables for dbservers group
host_vars/
web1.yml # Variables specific to web1
db1.yml # Variables specific to db1Ansible automatically loads variables from these directories based on the inventory structure. No explicit references needed.
---
http_port: 80
https_port: 443
app_root: /var/www/app
nginx_worker_processes: auto
log_level: infoThese variables apply to all hosts in the webservers group. They can be overridden by host_vars or playbook vars.
Extra Variables
Extra variables (--extra-vars or -e) are passed at the command line and always take highest precedence. They override everything else.
# Single variable
ansible-playbook site.yml -e "env=staging"
# Multiple variables
ansible-playbook site.yml -e "env=staging version=2.1.0"
# From a file
ansible-playbook site.yml -e "@vars/staging.yml"
# From JSON
ansible-playbook site.yml -e '{"env": "staging", "debug": true}'Extra variables are ideal for CI/CD pipelines where you need to override defaults without modifying files.
Extra variables have the highest precedence in Ansible. They override all other variable sources including role defaults, inventory vars, and playbook vars. Use them for environment-specific overrides.
Precedence Rules
Ansible defines 22 levels of variable precedence. Higher levels override lower ones. You rarely need to memorize all 22 — focus on the most common sources.
| Precedence | Source | Example |
|---|---|---|
| Highest | Extra vars (-e) | ansible-playbook site.yml -e env=prod |
| High | Play vars | vars: { env: prod } |
| Medium | Inventory vars | host_vars/web1.yml |
| Medium | group_vars | group_vars/webservers.yml |
| Low | Role defaults | roles/app/defaults/main.yml |
| Lowest | Inventory parameters | ansible_port=22 |
Understanding precedence prevents surprises. If a variable is not what you expect, check the precedence hierarchy. Use ansible-doc -t var-precedence for the full list.
Variable Best Practices
- Use group_vars for group-level configuration
- Use host_vars sparingly — only for host-specific overrides
- Use role defaults for safe, overridable defaults
- Use extra-vars for CI/CD environment parameters
- Prefix variables with app_ or role_ to avoid namespace collisions
- Document all variables with comments in defaults/main.yml
Use ansible all -m debug -a 'var=hostvars[inventory_hostname]' to inspect all variables for a host. This reveals where each variable comes from.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.