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.


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.


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!

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn more about Ansible automation with practical examples in my Academy:
Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

Check out my book, Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrators and DevOps:

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code


Help keep this project going by supporting me:
Patreon Buy me a Pizza