Ansible variable precedence is the order Ansible uses to decide which value wins when the same variable is defined in several places. Ansible’s documentation lists 22 levels. Role defaults have the lowest precedence and extra vars passed with -e on the command line always have the highest.
A simplified order, from lowest to highest: role defaults, inventory group vars, inventory host vars, play vars, role vars, task vars, set_fact and registered vars, extra vars.
Magic variables are variables Ansible sets automatically, such as inventory_hostname, hostvars, groups and ansible_check_mode. You can read them but shouldn’t define your own variables with those names.
Precedence surprises are a common source of Playbook bugs. Keep variables in as few places as possible and put overridable values in role defaults.