Middle
How is the hierarchy of roles in DevOps structured?
sobes.tech AI
Answer from AI
Variables in Ansible have a strict priority hierarchy. Ansible merges variables from various sources, overwriting lower-priority values with higher-priority ones.
Hierarchy (from lowest to highest priority):
- Role defaults: Variables defined in
defaults/main.ymlwithin a role. They have the lowest priority, intended for default values that can be easily overridden. - Role vars: Variables defined in
vars/main.ymlwithin a role. They have a higher priority than role defaults. - Inventory variables:
- Global inventory variables (
group_vars/all/host_vars/allor in the main inventory file). - Inventory group variables (
group_vars/<group_name>). Priority depends on the order of groups in the inventory and playbook, but generally, more deeply nested groups or those defined later have higher priority. - Inventory host variables (
host_vars/<host_name>or in the main inventory file). They have a higher priority than group variables.
- Global inventory variables (
- Play variables: Variables defined directly in the
varssection of a play. - Play vars_files: Variables loaded into the play via
vars_files. Their order determines their priority. - Facts: Variables gathered after executing the
gather_factsmodule. They have high priority as they represent the current system state. - Task variables: Variables set via
registerafter task execution. They have high priority and are only available within the current play after the task. - Command line variables: Variables passed using the
-eor--extra-varsflag when runningansible-playbook. They have the highest priority and overwrite all other variables.
Example of a playbook with variables:
---
# main.yml playbook
- hosts: webservers
vars:
global_var: "global_value_from_play" # Play variable (priority 4)
roles:
- webservers # Role with defaults/main.yml and vars/main.yml (priorities 1 and 2)
tasks:
- name: Print variable
debug:
msg: "Variable value is: {{ my_variable }}"
vars: # Task variable (priority 7)
my_variable: "task_specific_value"
Example inventory:
# inventory
[webservers]
webserver1 ansible_host=192.168.1.10 # Host variable (priority 3)
[all:vars]
my_variable="inventory_all_value" # Global inventory variable (priority 3)
Example role defaults:
# roles/webservers/defaults/main.yml
---
my_variable: "default_value_from_role" # Role defaults variable (priority 1)
Example role vars:
# roles/webservers/vars/main.yml
---
my_variable: "vars_value_from_role" # Role vars variable (priority 2)
When running the playbook ansible-playbook -i inventory main.yml -e "my_variable=extra_vars_value":
- The value from
-ewill be used:extra_vars_value(priority 8). - If
-eis not present, but there is a task variable, it will be used:task_specific_value(priority 7). - If neither task variable nor
-eis present, a fact will be used (if applicable). - Then, the play variable:
global_value_from_play(priority 4). - Then, the host variable from inventory:
ansible_host=192.168.1.10(priority 3). - Then, the group variable from inventory (
group_varsor in inventory). - Then, the global inventory variable:
inventory_all_value(priority 3). - Then, the role vars:
vars_value_from_role(priority 2). - Finally, the role defaults:
default_value_from_role(priority 1).
Ansible takes the first found value with the highest priority from this hierarchy. Understanding this hierarchy is critical for debugging and proper configuration management.