Using jinja macros in tasks templates

Hi!

The case is, I want to define some simple jinja macros, to simplify some common filters / conditionals combinations without using python directly (my point is: supporting those tasks is easier for my team than supporting full featured python code).

Is there a way to use custom jinja macros inside tasks templating engine?

Eg:

{% macro domit(value) -%}
{{ value if value else omit }}

{%- endmacro %}

then:

  • script: runme.sh
    args:
    creates: “{{ domit(my_var) }}”

You can, but it has to be inline with the template where it is used. As such, it is not very useful, since it cannot be reused.

As such, our recommendation is to create custom filter plugins instead of using macros.