@Zeitounator response helped me a lot, and I'd like to give more details here (in a comment, the format is too limited), but big thanks to @Zeitounator! :)
To give a concrete example, in your role:
roles
`-- passenger
|-- defaults
| `-- main.yml
|-- tasks
| `-- main.yml
`-- templates
`-- passenger.j2
In the defaults/main.yml
:
passenger_config_template: passenger.j2
In the tasks/main.yml
:
- name: Template passenger to /etc/passenger
ansible.builtin.template:
src: "{{ passenger_config_template }}"
dest: /etc/passenger
By default, Ansible will try to find the file in the role's directory templates
for the ansible.builtin.template
module (and files
directory for the ansible.builtin.copy
module)
More about Ansible path resolution: https://docs.ansible.com/ansible/latest/user_guide/playbook_pathing.html
Now, in your playbook, to overwrite the default role's template passenger.j2
, you would have this directory layout:
your_ansible_directory
|-- templates
| `-- passenger.j2 <-- your custom template
|-- ansible.cfg
`-- playbook.yml
In the playbook.yml
:
- hosts: your_host
tasks:
- ansible.builtin.import_role:
name: passenger
vars:
passenger_config_template: "{{ playbook_dir }}/templates/passenger.j2"
Note: I've renamed the role name on purpose to give an example of a good role name: role names shouldn't be prefixed by ansible-
or ansible-role-
and should use only _
(underscore) characters, not -
(hypen) or .
(dot) characters.
Read more about role naming:
https://galaxy.ansible.com/docs/contributing/creating_role.html#role-names