How can I set directives shared between all tasks in the file, in an Ansible playbook fragment file that only contains tasks?
# ansible/inventory/roles/os_packages/tasks/main.yaml
- name: "APT: Update available packages from Debian repositories"
gather_facts: false
become: true
become_user: root
apt:
update_cache: true
- name: "APT: Install required packages"
gather_facts: false
become: true
become_user: root
apt:
name:
- foo
- bar
Those repeated directives — gather_facts
, become
, etc. — should be stated one time in that file. But where, since there is (currently?) no file representing the role or play?
# ansible/inventory/roles/os_packages/tasks/main.yaml
# This doesn't work because Ansible is expecting only tasks.
gather_facts: false
become: true
become_user: root
- name: "APT: Update available packages from Debian repositories"
apt:
update_cache: true
- name: "APT: Install required packages"
apt:
name:
- foo
- bar
The file needs to consist only of a sequence of tasks; the “role” level or “play” level don't appear to have an appropriate place in this recommended directory structure.
If the entire playbook was in one file, each play would have its own separate name and I'd set the directives on the play. But these tasks files are being gathered and compiled, and there's no place where the play exists for me to write those directives.
Where should the directives that apply to all tasks in a role get defined?
My typical directory structure:
In this case, I put such directives in the top-level play
deploy-something.yml
:gather_facts
is not meaningful (or possible) to set for anything other than a play, because it only affects the play-level decision of whether to gather facts.become
andbecome_user
are valid block- or task-level keywords, so you can use a block to apply them to a list of tasks (within a role or otherwise):Many keywords can also be applied when calling roles:
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
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 belowQ: "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 bothbecome
andbecome_user
apply to the role.