Ansible: Automate Everything from First Playbook to Production · Variables, Facts and Jinja2 Templates

Variables: Defaults, Vars, Extra-vars and Precedence

Define variables at every level — inventory, playbook, role defaults, role vars, extra-vars — and predict which value wins when two levels set the same name.

Members can save this page, take notes and track progress. See membership · Sign in

What you'll build

A playbook that deploys an application whose listen port, worker count and log level differ between staging and production — controlled entirely by variable precedence, not when conditionals.

The idea

Ansible has 22 places where a variable can be defined. In practice you only need to remember five tiers (lowest to highest priority):

  1. Role defaults (roles/x/defaults/main.yml) — safe fallbacks the consumer can override.
  2. Inventory variables (group_vars/, host_vars/) — per-environment values.
  3. Playbook vars (vars: in a play) — values scoped to one play.
  4. Role vars (roles/x/vars/main.yml) — internal constants the consumer should not override.
  5. Extra-vars (-e on the command line) — the nuclear option, beats everything.

The rule: the more specific the scope, the higher the priority. Extra-vars win because they are the most specific — the operator typed them right now.

Dot notation (app.port) and bracket notation (app['port']) both access nested dictionaries. Bracket notation is safer when a key contains a dot or clashes with a Python attribute.

set_fact creates a variable at runtime whose priority sits between task vars and extra-vars — useful for computed values like a release timestamp.

The code

Variable precedence in action
mkdir -p precedence/roles/app/defaults precedence/roles/app/vars precedence/roles/app/tasks precedence/group_vars

cat > precedence/roles/app/defaults/main.yml <<'EOF'
app_port: 8080
app_workers: 2
app_log_level: info
app_name: myapp
EOF

cat > precedence/roles/app/vars/main.yml <<'EOF'
# Internal constant — consumers should not change this
app_user: www-data
EOF

cat > precedence/roles/app/tasks/main.yml <<'EOF'
- name: Show resolved configuration
  ansible.builtin.debug:
    msg: >-
      {{ app_name }} listens on :{{ app_port }},
      {{ app_workers }} workers, log={{ app_log_level }},
      user={{ app_user }}
EOF

cat > precedence/group_vars/production.yml <<'EOF'
app_port: 443
app_workers: 8
app_log_level: warning
EOF

cat > precedence/group_vars/staging.yml <<'EOF'
app_port: 8443
app_workers: 2
app_log_level: debug
EOF

cat > precedence/inventory.ini <<'EOF'
[staging]
localhost ansible_connection=local

[production]
localhost ansible_connection=local
EOF

cat > precedence/deploy.yml <<'EOF'
- name: Deploy application
  hosts: staging
  gather_facts: false
  roles:
    - app
EOF

cd precedence
ansible-playbook -i inventory.ini deploy.yml 2>/dev/null

Output

Run this on your computer — see “Get Started” for setup.

The staging group_vars override the role defaults (port 8443, log debug). Now override with extra-vars:

Extra-vars beat everything
cd precedence
ansible-playbook -i inventory.ini deploy.yml -e 'app_port=9999 app_log_level=trace' 2>/dev/null

Output

Run this on your computer — see “Get Started” for setup.

Extra-vars (-e) won over both the group_vars and role defaults.

You can also compute variables at runtime with set_fact:

- name: Compute a release tag
  ansible.builtin.set_fact:
    release_tag: "{{ app_name }}-{{ ansible_date_time.iso8601_basic_short }}"

- name: Show the computed tag
  ansible.builtin.debug:
    msg: "Deploying {{ release_tag }}"

Common mistakes

Putting secrets in defaults/main.yml — defaults are meant to be overridden and are visible in version control. Use ansible-vault for secrets and load them from group_vars/ or vars/main.yml.

Trying to override role vars from inventory — role vars (roles/x/vars/main.yml) have higher precedence than inventory variables. If a role author put a value in vars/, they intended it to be a constant. Move it to defaults/ if consumers need to change it.

Redefining a variable with the same name at two levels without understanding who wins — when a deploy behaves differently than expected, run with -e to confirm the value, then trace where the winning definition lives.

Key points

  • Five precedence tiers matter: role defaults → inventory (group/host vars) → playbook vars → role vars → extra-vars.
  • set_fact creates runtime variables that persist for the rest of the play.
  • Use role defaults/ for values consumers should override and role vars/ for internal constants.
  • Extra-vars (-e) always win — useful for one-off overrides and CI pipelines.

Practice questions

A role sets `app_port: 8080` in defaults/main.yml and a group_vars file sets `app_port: 443`. Which value does Ansible use?

Answer

443 — inventory group_vars have higher precedence than role defaults.

What is the highest-precedence variable source in Ansible?

Answer

Extra-vars passed with -e on the command line beat every other source.

When should you use `set_fact` instead of defining a variable in a vars file?

Answer

When the value is computed at runtime — for example, a timestamp, a derived path, or a value read from a previous task's output.

Open in the app to mark lessons complete, take notes and track your progress.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake