Interview Prep/Ansible/Variables, Facts and Precedence

What is the difference between a role's `defaults/` and `vars/`?

IntermediateConceptVariables, Facts and Precedence
Answer

defaults/main.yml has the lowest precedence, so inventory, play or -e can override it. It is the role's public interface. vars/main.yml has high precedence (level 15) and is hard to override from inventory, so use it for internal constants (such as OS-specific package names) that users should not change.

# roles/nginx/defaults/main.yml
nginx_worker_processes: auto
# roles/nginx/vars/Debian.yml
nginx_pkg: nginx-full

If users cannot override something that they reasonably need to, you put it in the wrong place.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence