Menu

Ansible Templates and Handlers

Ansible Templates and Handlers

Introduction to Jinja2 Templates and Dynamic Configuration Files

Templates use Jinja2 expressions to render configuration files from variables and facts. Store templates in a templates/ directory and use the template module to render them on managed hosts.

Keep templates readable and avoid embedding complex business logic in them. Validate the generated configuration with the service's own validation command where possible.

Using Ansible Handlers to Manage Service Restarts and Configuration Changes

Handlers run when notified by a task that reports a change. This avoids restarting a service after every task or when the configuration has not changed.

Handlers normally run at the end of a play. Use listen topics to group related notifications and flush_handlers only when a restart must happen before later tasks.

Working with Jinja2 Filters, Tests and Template Variables

EXAMPLE TEMPLATE VARIABLES

server_name: app.example.internal

listen_port: 8080

upstream_servers:

         -10.0.1.10:8080

         -10.0.1.11:8080

Filters transform values, for example, to format strings or provide defaults. Tests check value types or conditions. Use the current Ansible templating documentation because supported filters and syntax can vary by version.


Ansible Tutorial

HA

Harini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details