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:
- Extra Vars (from the command line with
-e) - Lookup Variables (e.g., using
{{ lookup(...) }}) - Reserved Variables (predefined Ansible variables, such as
hostvars,group_names,inventory_hostname) - Inventory Variables
- Group variables (
group_vars/) - Host variables (
host_vars/)
- Group variables (
- Role Variables (
roles/<role_name>/vars/) - Play Variables (
vars:in the playbook) - Role Defaults (
roles/<role_name>/defaults) - 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.