DevOps Bootcamp · Lesson

defaults vs vars: The Override Order

Design clean role variable interfaces.

Lesson 2 of 413 steps

defaults vs vars: The Override Order is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 2 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.

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: 4

vars 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/myapp

Defaults 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: 8080

Overriding 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=9090

The 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. 🎚️

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 “defaults vs vars: The Override Order” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “defaults vs vars: The Override Order”, 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 “defaults vs vars: The Override Order”?

Design clean role variable interfaces. 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 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 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. Role Dependencies in meta/main.yml
  2. defaults vs vars: The Override Order
  3. include_role & import_role at Runtime
  4. allow_duplicates & Role Idempotency
← Back to DevOps Bootcamp