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.
More on Roles, Collections and Project Structure
You have a 600-line playbook that sets up web, db and monitoring. How do you restructure it?BeginnerWhere do you put a variable users of your role are expected to override?BeginnerTwo roles need to run in a fixed order, such as `common` before `nginx`.BeginnerA role is used twice with different parameters in one play, but only runs once.IntermediateYou want to share roles across several repos and teams.IntermediateHow do you pin Ansible itself so a CI run today behaves like one last month?Intermediate