Junior — Middle
What is the reason for having two separate directories with variables when using the Ansible role?
sobes.tech AI
Answer from AI
In Ansible, a role can contain two separate directories for variables: vars and defaults.
vars— variables placed here have a higher priority. They are usually set explicitly and are not intended to be overridden by the user.defaults— contains default variables with a lower priority. They can be easily overridden when calling the role or elsewhere in the playbook.
This separation allows setting base variable values that can be changed without touching the main role settings. It increases flexibility and reusability of roles.
Example structure:
roles/myrole/
defaults/main.yml # default variables
vars/main.yml # mandatory or fixed variables
Thus, having two directories for variables helps manage priorities and configuration convenience.