DevOps Bootcamp · Lesson

Where Variables Come From & Precedence

A first map of variable sources.

Lesson 4 of 413 steps

Where Variables Come From & Precedence is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 4 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.

So Many Sources

The same variable can be set in many places: inventory, play vars, files, the CLI and more. So which value actually wins? 🌟

Precedence Decides

Ansible follows a fixed order called variable precedence. When a name is set twice, the higher-priority source overrides the lower one.

Role Defaults Are Lowest

Values in a role's defaults folder sit at the very bottom. They are meant to be easy to override by almost anything else.

# roles/web/defaults/main.yml
http_port: 80

Inventory Variables

Vars set in your inventory, like in group_vars or host_vars, rank above role defaults but below play-level vars.

# group_vars/web.yml
http_port: 8080

Play vars Sit Higher

Variables under a play's vars key outrank inventory values. Define a value here and it beats anything in group_vars.

- hosts: web
  vars:
    http_port: 9090

Task vars Are Narrow

You can attach vars to a single task. That value applies only there and ranks above broader play vars.

- debug:
    var: http_port
  vars:
    http_port: 1234

Registered & set_fact

Values you capture with register or set during a run with set_fact sit high in the order, just under extra vars.

- set_fact:
    http_port: 7000

Extra Vars Are the Top

Command-line extra vars beat everything else. Whatever you pass with -e is the final word for that run.

ansible-playbook site.yml -e http_port=443

Last Definition Wins, Within a Tier

When two sources share the same level, the one loaded last takes effect. Precedence first, load order to break ties.

Keep It Simple

You rarely need to memorize the whole chart. Put sensible values in defaults, override deliberately, and avoid setting the same name everywhere.

Debug What You Got

Unsure which value won? Print it with debug to see the resolved result before trusting it in production.

- debug:
    var: http_port

Quick Check

Which source of a variable has the highest precedence?

Recap

You mapped the precedence ladder: defaults lowest, then inventory, play and task vars, then set_fact, with extra vars on top. ✨

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 “Where Variables Come From & Precedence” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Where Variables Come From & Precedence”, 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 “Where Variables Come From & Precedence”?

A first map of variable sources. 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 4 of 4.

How long does the “Where Variables Come From & Precedence” 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. Defining & Using vars in a Play
  2. The {{ }} Substitution Syntax
  3. Extra Vars on the Command Line
  4. Where Variables Come From & Precedence
← Back to DevOps Bootcamp