AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Can You Use Ansible's assert Module in Jinja Templates?

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

This article explains why Ansible's assert module cannot be used directly in Jinja templates and showcases alternative validation methods for effective.

Introduction

Ansible offers powerful tools for automating IT workflows, but understanding the proper use of its features is essential. One common question is whether Ansible's assert module can be used directly inside Jinja templates. The short answer: no. Jinja templates are designed for rendering, not for executing tasks. However, there are ways to achieve similar functionality. This article explores alternative validation methods that align with the purpose of Jinja templates and Ansible tasks.

---

See also: Ansible troubleshooting - Error: name[template]

Why You Can't Use assert in Jinja Templates

The assert module in Ansible is designed for use in tasks to validate conditions, not for use in Jinja templates, which focus on rendering data. Jinja templates provide conditional logic and filters that allow for validation-like behavior, but they lack the ability to interact with Ansible modules.

---

Validation Examples in Jinja Templates

Example 1: Raising an Error in Jinja

To simulate an assertion failure, you can intentionally raise an error within a Jinja template:

{% if my_var is not defined or my_var | int <= 10 %}
  {% set _ = (1 / 0) %}  # This raises an intentional division by zero error.
{% endif %}

This approach stops execution if my_var is either undefined or less than or equal to 10.

---

Example 2: Returning a Custom Error Message

Instead of stopping execution, you can return a meaningful error message to help diagnose the problem:

{% if my_var is not defined or my_var | int <= 10 %}
  ERROR: my_var must be defined and greater than 10
{% else %}
  Valid value: {{ my_var }}
{% endif %}

This method is helpful when generating reports or debugging templates.

---

See also: Ansible troubleshooting - Error no-prompting

Combining Jinja with Ansible's assert Module

For robust validations, use Jinja expressions in combination with Ansible's assert module in your playbook.

Example 3: Validating Variables with assert

- name: Assert that my_var is valid
  ansible.builtin.assert:
    that:
      - "{{ my_var is defined }}"
      - "{{ my_var | int > 10 }}"
    fail_msg: "'my_var' must be defined and greater than 10."

This task validates that my_var is defined and greater than 10. If the condition fails, Ansible stops execution with a descriptive error message.

---

Conclusion

While Jinja templates cannot execute Ansible's assert module, they can perform basic validations using conditional logic. For more complex error handling, use the assert module in your playbook. This separation ensures a clean design and improves playbook maintainability.

Mastering the interplay between Jinja templates and Ansible tasks empowers you to create reliable automation workflows. Need help implementing these techniques? Let me know!

See also: API Validation with Postman: Automate Tests and CI Integration Guide (2026)

Related Articles

Jinja2 templating in Ansibleregister and when in Ansible

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home