Sobes.tech
Senior

What is the priority order of variables in Ansible? Compare defaults and vars.

sobes.tech AI

Answer from AI

In Ansible, variables have different priorities that determine which values are used in case of conflicts. Specifically, defaults and vars differ in priority:

  • defaults are variables with the lowest priority. They are usually defined in roles in the defaults/main.yml file. They set default values that are easy to override.

  • vars are variables with a higher priority, defined in the vars/main.yml of roles or in vars blocks within playbooks. They override values from defaults.

Thus, if the same variable is defined in both defaults and vars, the value from vars will be used. This allows setting base values and overriding them with more specific settings if needed.