Junior — Middle
What are the differences between the vars and defaults folders in the Ansible structure, and what are their purposes?
sobes.tech AI
Answer from AI
In Ansible, the vars and defaults folders are used to store variables, but with different priorities and purposes:
-
defaults — contains variables with the lowest priority. Usually, default values are placed here, which can be overridden elsewhere (for example, in
vars, in inventory, or at runtime). -
vars — contains variables with a higher priority than
defaults. Variables fromvarshave a higher priority than those fromdefaultsand are typically used for mandatory or specific settings.
Example:
If in a role, defaults/main.yml specifies:
app_port: 8080
And in vars/main.yml:
app_port: 9090
Then the value 9090 will be used.
Thus, defaults are basic values, and vars are more specific, capable of overriding defaults.