What are variables in Ansible and how does variable precedence work?
Understand Ansible variables and the precedence order from role defaults to extra vars, with examples of overriding values and debugging which one wins.
Expected Interview Answer
Variables in Ansible are named values that make playbooks reusable and dynamic, and variable precedence is the fixed set of rules Ansible uses to decide which value wins when the same variable is defined in more than one place.
Variables can come from many sources — role defaults, inventory group_vars and host_vars, play vars, facts, registered results, set_fact, and the command line. When a variable is defined in multiple places, Ansible applies a documented precedence order from lowest to highest: role defaults are the weakest, and extra vars passed with `-e` on the command line always win. Understanding this order is essential for predicting behavior and avoiding surprises where a value you set is silently overridden.
- Makes playbooks reusable across hosts and environments
- Lets roles ship sensible defaults that callers can override
- Separates configuration data from automation logic
- Command-line extra vars give a reliable final override for one-off runs
- Clear precedence rules make behavior predictable and debuggable
AI Mentor Explanation
Variable precedence is like the chain of authority over a batting order. The team's default order is printed on the sheet (role defaults), the coach can adjust it for a venue (group vars), the captain tweaks it on match day (play vars), but the umpire's ruling or a last-minute selectors' note (extra vars with -e) overrides everyone. When two instructions clash, the higher authority's call is the one that actually takes the field.
Step-by-Step Explanation
Step 1
Define role defaults
Put the weakest, overridable baseline values in a role's defaults/main.yml.
Step 2
Add inventory vars
Set environment-specific values in group_vars and host_vars, which override role defaults.
Step 3
Set play and block vars
Use vars, vars_files, or block-level vars in the play, which sit above inventory in precedence.
Step 4
Register and set_fact
Capture runtime data with register or set_fact; these are high-precedence host-scoped variables.
Step 5
Override with extra vars
Pass `-e key=value` on the command line for the final, highest-priority override during a run.
Step 6
Debug conflicts
When a value is unexpected, use the debug module to see which source actually won.
What Interviewer Expects
- Knows the many sources variables can come from
- Can order at least role defaults (lowest) to extra vars (highest)
- Understands set_fact and registered vars are high precedence
- Explains overriding role defaults via group_vars/host_vars
- Can debug which definition won using the debug module
Common Mistakes
- Thinking group_vars always beats play vars (it doesn't)
- Assuming role defaults are hard to override when they are the weakest
- Not knowing extra vars (-e) always win
- Confusing variable precedence with task execution order
- Overusing set_fact where simple defaults would be clearer
Best Answer (HR Friendly)
“Ansible variables are named settings that let one playbook work in many situations. Because the same setting can be defined in several places, Ansible has a clear pecking order for which value wins — with values you type on the command line always taking top priority.”
Code Example
# roles/web/defaults/main.yml (lowest precedence)
http_port: 80
# group_vars/production.yml (overrides role defaults)
http_port: 8080
# playbook.yml play vars (overrides inventory)
- hosts: web
vars:
http_port: 9090
roles:
- web
# Command line always wins:
# ansible-playbook playbook.yml -e "http_port=443"- hosts: web
tasks:
- name: Check service
ansible.builtin.command: systemctl is-active nginx
register: nginx_status
changed_when: false
- name: Show which port won
ansible.builtin.debug:
msg: "port={{ http_port }} status={{ nginx_status.stdout }}"Follow-up Questions
- What is the full Ansible variable precedence order from lowest to highest?
- How do set_fact and registered variables differ in scope?
- When would you use vars_files instead of group_vars?
- How does variable scoping differ between host, play, and global?
- How do you safely override a role's default without editing the role?
MCQ Practice
1. Which variable source has the highest precedence?
Command-line extra vars (-e) sit at the top of Ansible's precedence order and override every other source.
2. Which variable source has the lowest precedence?
Role defaults are intentionally the weakest so callers can override them from almost anywhere.
3. Where do registered variables from `register:` live?
Registered results are stored as host-scoped variables and have high precedence during the play.
Flash Cards
Lowest-precedence variable source? — Role defaults defined in defaults/main.yml.
Highest-precedence variable source? — Extra vars passed on the command line with -e.
What does register do? — Captures a task's result into a high-precedence, host-scoped variable for later use.
How to override a role default per environment? — Set the variable in group_vars or host_vars — no need to edit the role.