LOGBOOK

HELP

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 generic task file with package and service variables, called twice: once with httpd for a web server and once with mariadb for a database server

* 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:

From Quiz: ITIA / Ansible: Complex Playbooks with Imports and Includes | Updated: Nov 26, 2026