Interview Prep/Ansible/Roles, Collections and Galaxy

A role fails on Rocky Linux 9 with `Could not find or access 'Rocky.yml'` from an OS-specific include

IntermediateTroubleshootingRoles, Collections and Galaxy
Answer

Likely causes → The role includes {{ ansible_facts['distribution'] }}.yml, and only RedHat.yml, CentOS.yml and Ubuntu.yml exist.

Fix → Fall back from the most specific file to the most generic:

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

Prevent → Key OS differences on os_family first, and test new distributions in the Molecule platform matrix.

All Ansible questions Roadmap: Ansible

More on Roles, Collections and Galaxy