Sobes.tech
Junior — Middle

Which variables have the highest priority when using Ansible?

sobes.tech AI

Answer from AI

In Ansible, variables have a specific priority that affects which values are used in case of conflicts. The highest priority is given to variables defined directly in the playbook or in the vars block, followed by variables defined in the inventory, and then variables from roles and facts.

In general, the priority of variables in Ansible (from highest to lowest) is approximately:

  1. Variables passed through the command line (e.g., using -e or --extra-vars).
  2. Variables defined in the playbook in the vars block.
  3. Variables defined in roles (vars/main.yml).
  4. Variables from the inventory.
  5. Facts collected from hosts.

Thus, if the same variable is defined in multiple places, the value with the highest priority will be used. This allows flexible management of configuration and overriding values when necessary.