Sobes.tech
Middle

What is the hierarchy of variables in different DevOps roles?

sobes.tech AI

Answer from AI

The hierarchy of variables in DevOps most often relates to configuration management systems such as Ansible, Puppet, or Chef, where different levels of variable definition have different priorities. In the context of Ansible, it looks like this:

  1. Extra Vars (from the command line with -e)
  2. Lookup Variables (e.g., using {{ lookup(...) }})
  3. Reserved Variables (predefined Ansible variables, such as hostvars, group_names, inventory_hostname)
  4. Inventory Variables
    • Group variables (group_vars/)
    • Host variables (host_vars/)
  5. Role Variables (roles/<role_name>/vars/)
  6. Play Variables (vars: in the playbook)
  7. Role Defaults (roles/<role_name>/defaults)
  8. Inventory Variables (from the inventory file)
    • Group variables in the inventory
    • Host variables in the inventory

Example of defining variables in Ansible for a role:

---
# roles/my_role/vars/main.yml
my_variable: "value set in role vars"

Example of defining default variables for a role:

---
# roles/my_role/defaults/main.yml
my_variable: "default value"
another_variable: "default specific to role"

Example of usage in a playbook:

---
- name: Apply my_role
  hosts: my_servers
  vars:
    my_variable: "value set in playbook"
  roles:
    - my_role

Example of a host variable in the inventory:

[my_servers]
server1 my_variable="value from inventory host"
server2

Variables defined at higher levels in this hierarchy (e.g., Extra Vars) override variables defined at lower levels (e.g., Role Defaults).

In other tools, the hierarchy may differ, but the general principle of variable priority, especially those defined closer to the usage or explicitly specified (like in the command line), remains similar.