Interview Prep/Ansible/Roles, Collections and Project Structure

A role has a task that should run only if the user opts in, e.g. install the monitoring agent.

BeginnerScenarioRoles, Collections and Project Structure
Answer

Situation read → Feature toggle.

What I'd do →

# defaults/main.yml
app_enable_monitoring: false
# tasks/main.yml
- ansible.builtin.import_tasks: monitoring.yml
  when: app_enable_monitoring | bool

Why → Boolean flags in defaults give a documented switch.

Interview tip → | bool guards against the string "false" passed with -e.

All Ansible questions Roadmap: Ansible

More on Roles, Collections and Project Structure