Ansible Series

🔀 Understanding Ansible Variable Precedence From a "Top 1%" Perspective: When group_vars, host_vars, and extra-vars Collide, Which One Wins?

Introduction

This article is part of the Top 1% Series Complete Article Guide, the 14th article in the Ansible Series.

The Big Picture

graph TB
    A["role defaults<br/>(lowest priority)"]
    B["inventory group_vars"]
    C["inventory host_vars"]
    D["vars inside the Playbook"]
    E["role vars"]
    F["values set dynamically via set_fact"]
    G["-e / --extra-vars<br/>(highest priority)"]
    A --> B --> C --> D --> E --> F --> G

A Thorough, Grounds-Up Explanation

Six Common Places to Define a Variable

The Core Precedence Principle: “More Specific and More Recent” Wins

Ansible’s official documentation lists a detailed 21-tier variable precedence order, but what matters first in practice is one core principle: whatever applies to a narrower scope, or loads later, wins over whatever applies more broadly, or loads earlier. As the diagram shows, a “broad, early” variable like role defaults gets overridden easily, while -e, specified directly at every single run, overrides everything else.

What a Pro Sees Here (Top 1% Understanding)

“I Passed -e and It Didn’t Take Effect” Almost Never Happens — Which Is What Makes It Dangerous

-e (extra-vars) effectively holds the strongest power in Ansible’s entire variable-precedence order. Flip that around, and it means: pass even one wrong value via -e at Playbook runtime, and it overrides everything else without exception, no matter how carefully you’ve configured role defaults or group_vars. In a setup where a CI/CD pipeline invokes Ansible, a classic incident shape is a pipeline misconfiguration that passes an unintended -e, applying a staging value to the production environment.

Precisely because of that strength, the standard practice in real-world operations is to keep -e limited to narrow uses — a one-off override, or an explicit injection from CI/CD — and manage persistent configuration values through group_vars or host_vars instead. Conversely, since role defaults is the place meant for “an initial value the user is free to override,” a value the role genuinely needs to stay fixed belongs in vars (not defaults), which avoids it getting overridden from something like group_vars.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. An unexpected variable value is being used: Run ansible-playbook with -v (verbose output), or use ansible-inventory --host <hostname> to check the final value that ends up applied to that host.
  2. Only runs via a CI/CD pipeline apply an unintended setting: Check whether the pipeline’s run command embeds an unintended -e argument.
  3. Reusing a role doesn’t pick up the expected defaults value: Check whether the caller (a Playbook or group_vars) defines a variable with the same name.

Summary

Takeaways to Apply Today

  1. Manage persistent configuration values through group_vars/host_vars, and keep -e limited to one-off overrides or CI/CD injection.
  2. When you hit an unexpected variable value, build the habit of checking ansible-inventory --host <hostname> to see which value actually won.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).