Q: "Where should the directives that apply to all tasks in a role get defined?"
A: Short answer is: Outside a role.
Where exactly you should put the keywords depends on how you use the role. There are more options
- roles:
- import_role:
- include_role:
It's out of scope to explain all details here. Generally, put such keywords on the (indentation) level of a role. In the case of include_role you can also apply keywords. Be aware that not all keywords can be applied everywhere. See Playbook Keywords that apply to roles. For example, the keyword gather_facts
can be applied to a playbook only. One of the options on how to solve your problem might be the structure of the play below
- hosts: all
gather_facts: false
roles:
- role: os_packages
become: true
become_user: root
Q: "How to do that (apply such keywords on the level of a role), when there is no config file (only a role directory) representing the role?"
A: If there is only the role directory, representing the role, you can apply keywords to the blocks and tasks inside the role only. You can't apply keywords to a role inside this role. There is no such thing as a config file for a role.
In the previous answer, by level I mean the indentation level, for example in the code above, gather_facts
apply to the play while both become
and become_user
apply to the role.