Quiz Entry - updated: 2026.11.26
How do you make a task file reusable for other software than the one it was written for?
Replace the fixed values with variables, such as {{ package }} and {{ service }}, and pass the values with vars: where the file is imported or included.
* One task file, different values: the caller decides what gets installed. *
A task file that installs and starts httpd is useful only for Apache. With variables it works for any package that comes with a service:
# task.yml
- name: Install the {{ package }} package
ansible.builtin.dnf:
name: "{{ package }}"
state: latest
- name: Start the {{ service }} service
ansible.builtin.service:
name: "{{ service }}"
enabled: true
state: started
The playbook supplies the values when it brings the file in:
tasks:
- name: Import task file and set variables
ansible.builtin.import_tasks: task.yml
vars:
package: httpd
service: httpd
The tasks from task.yml see these variables. The same file can be imported again with package: mariadb, service: mariadb. Note the quotes around "{{ package }}": a YAML value that starts with { must be quoted.
Go deeper:
Imports: static reuse (official docs) — why import file names need values known at parse time, and passing vars to imports.
When to quote variables, a YAML gotcha (official docs) — why a value that starts with {{ must be quoted.