Interview Prep/Ansible/Variables, Facts and Precedence

What is the difference between `vars_files` and `include_vars`?

IntermediateConceptVariables, Facts and Precedence
Answer

vars_files is a play keyword, loaded at play start (static), and the files must exist. include_vars is a task that loads a file or directory at run time, so the path can use facts and loops, and it has a higher precedence.

- ansible.builtin.include_vars: "{{ ansible_facts['os_family'] }}.yml"
- ansible.builtin.include_vars:
    dir: vars/app
    extensions: [yml]

For an OS-specific file with a default fallback, combine it with lookup('ansible.builtin.first_found', ...).

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence