Interview Prep/Ansible/Variables, Facts and Precedence Problems

`Could not find or access 'prod.yml' Searched in: .../roles/app/vars/prod.yml ...` from `include_vars`

BeginnerTroubleshootingVariables, Facts and Precedence Problems
Answer

Likely causes → Relative paths in include_vars are resolved against the role's vars/ directory or the playbook's directory, not your current shell directory. The file may also be named after a value that does not exist ({{ env }}.yml with env unset or misspelled).

Diagnose → The error lists every path it searched. Compare them with where the file actually is.

Fix → Put the file in roles/app/vars/, or use "{{ playbook_dir }}/vars/{{ env }}.yml". For optional files, use the first_found lookup with a fallback:

- ansible.builtin.include_vars: "{{ lookup('ansible.builtin.first_found', params) }}"
  vars:
    params:
      files: ["{{ env }}.yml", default.yml]
      paths: ["{{ role_path }}/vars"]

Prevent → Validate env against an allowed list with assert before you use it in a path.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence Problems