Sobes.tech
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):

  1. Role defaults: Variables defined in defaults/main.yml within a role. They have the lowest priority, intended for default values that can be easily overridden.
  2. Role vars: Variables defined in vars/main.yml within a role. They have a higher priority than role defaults.
  3. Inventory variables:
    • Global inventory variables (group_vars/all / host_vars/all or 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.
  4. Play variables: Variables defined directly in the vars section of a play.
  5. Play vars_files: Variables loaded into the play via vars_files. Their order determines their priority.
  6. Facts: Variables gathered after executing the gather_facts module. They have high priority as they represent the current system state.
  7. Task variables: Variables set via register after task execution. They have high priority and are only available within the current play after the task.
  8. Command line variables: Variables passed using the -e or --extra-vars flag when running ansible-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":

  1. The value from -e will be used: extra_vars_value (priority 8).
  2. If -e is not present, but there is a task variable, it will be used: task_specific_value (priority 7).
  3. If neither task variable nor -e is present, a fact will be used (if applicable).
  4. Then, the play variable: global_value_from_play (priority 4).
  5. Then, the host variable from inventory: ansible_host=192.168.1.10 (priority 3).
  6. Then, the group variable from inventory (group_vars or in inventory).
  7. Then, the global inventory variable: inventory_all_value (priority 3).
  8. Then, the role vars: vars_value_from_role (priority 2).
  9. 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.