defaults vs vars: The Override Order
Design clean role variable interfaces.
defaults vs vars: The Override Order is a free Ansible Academy lesson on CoddyKit. This is lesson 2 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, and your progress syncs across the web and the CoddyKit app. The Ansible Academy course includes 4 lessons in total.
Two Variable Folders
A role can define variables in two places: defaults/main.yml and vars/main.yml. They look similar but behave very differently.
defaults Are Soft
Values in defaults/main.yml sit at the lowest precedence. They are gentle suggestions that almost anything else can override.
# defaults/main.yml
http_port: 80
workers: 4vars Are Strong
Values in vars/main.yml have high precedence. They are firm decisions the role insists on, and they are hard to override.
# vars/main.yml
config_path: /etc/myappDefaults Are Your Public API
Put anything a user might tune into defaults. It signals: change me freely. This is how you design a friendly, reusable role.
Vars Are Internal Constants
Reserve vars for values the role needs to function and users should not touch, like an internal path or a fixed package name.
Override a Default Easily
A user can override a default from group_vars, host_vars, the play, or the command line. The simplest override wins instantly.
roles:
- role: web
vars:
http_port: 8080Overriding a vars Value Is Hard
Role vars beat group_vars, host_vars, and play vars. Only extra-vars on the command line outrank them, so use vars sparingly.
Extra Vars Always Win
Variables passed with -e on the command line top every other source, including role vars. They are the ultimate override.
ansible-playbook site.yml -e http_port=9090The Mental Order
Remember the ladder: defaults at the bottom, then inventory and play vars, then role vars near the top, then extra-vars above all.
Design Tip
Favor defaults for almost everything. Reach for vars only when a value truly must stay locked. Flexible roles keep teams happy.
Document Your Defaults
Comment each value in defaults/main.yml. Since users will tune these, clear notes turn your role into self-explaining automation.
Quick Check
Think about which file a user can override most easily.
Recap
Use defaults for tunable values users override and vars for locked internal constants. Extra-vars on the CLI beat everything. 🎚️
Frequently Asked Questions
Is the “defaults vs vars: The Override Order” lesson free?
Yes — the full text of “defaults vs vars: The Override Order” is free to read here on the web. 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. The Ansible Academy course includes 4 lessons in total.
What will I learn in “defaults vs vars: The Override Order”?
Design clean role variable interfaces. 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, 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 “defaults vs vars: The Override Order” 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
- Role Dependencies in meta/main.yml
- defaults vs vars: The Override Order
- include_role & import_role at Runtime
- allow_duplicates & Role Idempotency