官术网_书友最值得收藏!

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:

  1. Extra vars (from command-line) always win.
  2. Task vars (only for the specific task).
  3. Block vars (only for the tasks within the block).
  4. Role and include vars.
  5. Vars created with set_fact.
  6. Vars created with the register task directive.
  7. Play vars_files.
  8. Play vars_prompt.
  9. Play vars.
  10. Host facts.
  11. Playbook host_vars.
  12. Playbook group_vars.
  13. Inventory host_vars.
  14. Inventory group_vars.
  15. Inventory vars.
  16. 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.

主站蜘蛛池模板: 田林县| 梅州市| 三门县| 遂宁市| 罗平县| 阆中市| 中方县| 东丰县| 宁远县| 太保市| 武穴市| 黑河市| 北辰区| 会东县| 南投县| 闽清县| 永平县| 和田县| 清水河县| 南雄市| 本溪| 黎城县| 诸城市| 彭州市| 东阳市| 樟树市| 周宁县| 东源县| 贵州省| 扎囊县| 揭东县| 云霄县| 章丘市| 张家港市| 宁远县| 江阴市| 霍山县| 海伦市| 阳原县| 兴安县| 团风县|