Variable precedence
As you learned in the previous section, there are a few major types of variables that can be defined in a myriad of locations. This leads to a very important question: what happens when the same variable name is used in multiple locations? Ansible has a precedence for loading variable data, and thus it has an order and a definition to decide which variable will win. Variable value overriding is an advanced usage of Ansible, so it is important to fully understand the semantics before attempting such a scenario.
Precedence order
Ansible defines the precedence order as follows:
- Extra
vars
(from command-line) always win. - Task
vars
(only for the specific task). - Block
vars
(only for the tasks within the block). - Role and include
vars
. - Vars created with
set_fact
. - Vars created with the
register
task directive. - Play
vars_files
. - Play
vars_prompt
. - Play
vars
. - Host facts.
- Playbook
host_vars
. - Playbook
group_vars
. - Inventory
host_vars
. - Inventory
group_vars
. - Inventory
vars
. - Role defaults.
Merging hashes
In the previous section, we focused on the precedence in which variables will override each other. The default behavior of Ansible is that any overriding definition for a variable name will completely mask the previous definition of that variable. However, that behavior can be altered for one type of variable, the hash. A hash variable (a dictionary in Python terms) is a dataset of keys and values. Values can be of different types for each key, and can even be hashes themselves for complex data structures.
In some advanced scenarios, it is desirable to replace just one bit of a hash or add to an existing hash rather than replacing the hash altogether. To unlock this ability, a configuration change is necessary in an Ansible config
file. The config entry is hash_behavior
, which takes one of replace, or merge. A setting of merge will instruct Ansible to merge or blend the values of two hashes when presented with an override scenario rather than the default of replace, which will completely replace the old variable data with the new data.
Let's walk through an example of the two behaviors. We will start with a hash loaded with data and simulate a scenario where a different value for the hash is provided as a higher priority variable.
Starting data:
hash_var: fred: home: Seattle transport: Bicycle
New data loaded via include_vars
:
hash_var: fred: transport: Bus
With the default behavior, the new value for hash_var
will be as follows:
hash_var: fred: transport: Bus
However, if we enable the merge behavior, we will get the following result:
hash_var: fred: home: Seattle transport: Bus
There are even more nuances and undefined behaviors when using merge, and as such, it is strongly recommended to only use this setting if absolutely needed.