0Pricing
Ansible Academy · Lesson

Where Variables Come From & Precedence

A first map of variable sources.

Where Variables Come From & Precedence is a free Ansible Academy lesson on CoddyKit — lesson 4 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. ✨

Frequently asked questions

Is the “Where Variables Come From & Precedence” lesson free?

Yes — the full text of “Where Variables Come From & Precedence” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. 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.

What will I learn in “Where Variables Come From & Precedence”?

A first map of variable sources. 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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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

  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 Ansible Academy