You have to ship a custom module and filter with your role.
AdvancedScenarioRoles, Collections and Project Structure
Answer
Situation read → Role-local plugins.
What I'd do → Put them in the role: roles/myrole/library/my_module.py, roles/myrole/filter_plugins/my_filters.py. They become available to that role and anything after it in the play.
For wider reuse, put them in a collection under plugins/modules and plugins/filter, then call them by FQCN.
Why → Collections give namespacing, versioning and documentation hooks.
Interview tip → Mention ansible-doc and DOCUMENTATION strings for modules.
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