Interview Prep/Ansible/Roles, Collections and Project Structure

A role installs a package and writes a config, but must support Ubuntu and Rocky Linux.

IntermediateScenarioRoles, Collections and Project Structure
Answer

Situation read → OS-specific variables.

What I'd do →

# tasks/main.yml
- ansible.builtin.include_vars: "{{ lookup('ansible.builtin.first_found', params) }}"
  vars:
    params:
      files:
        - "{{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_major_version'] }}.yml"
        - "{{ ansible_facts['os_family'] }}.yml"
        - default.yml
      paths: [vars]
- ansible.builtin.package: { name: "{{ nginx_pkg }}", state: present }

Why → The data differs per OS and the tasks stay the same.

Interview tip → This first_found pattern is what well-known community roles use.

All Ansible questions Roadmap: Ansible

More on Roles, Collections and Project Structure