LOGBOOK

HELP

Quiz Entry - updated: 2026.11.12

How do you include text in a template only under a condition?

With {% if condition %} ... {% endif %}: the enclosed text is written only if the condition is true, for example {% if finished %}{{ result }}{% endif %}.

{% if finished %}
{{ result }}
{% endif %}

If finished is true, the value of result appears in the file; otherwise nothing does. The condition uses the same expressions as when in a playbook: comparisons, and/or, tests like is defined, and {% elif %} / {% else %} are available too.

Typical uses in config files:

{% if ssl_enabled %}
SSLEngine on
SSLCertificateFile {{ ssl_cert }}
{% endif %}
{% if ansible_facts['os_family'] == "Debian" %}
Include /etc/apache2/conf-enabled/*.conf
{% endif %}

One template then serves hosts with different roles or distributions, instead of one template per variant.

Go deeper:

From Quiz: ITIA / Ansible: Managing Files and Jinja2 Templates | Updated: Nov 12, 2026