Complex computation within jinja-templates in roles?

Hi,

I'm seeking a way to have complex computation within jinja-templates in
roles. The computations I need are approx 100 lines of code, so using
the jinja2-syntx "{%....%} for each line is tremulous.

My first idea was to implement a python filter-plugin. But Ansible does
not search for plugins within the role's directory.

* Is there a way to include a block of Python-code into the template?

* Is there a way to directly call a Python-script or function located
relativly to the template?

* Any other solutions?

Hartmut Goebel <h.goebel@crazy-compilers.com> napisał:

Hi,

I'm seeking a way to have complex computation within jinja-templates in
roles. The computations I need are approx 100 lines of code, so using
the jinja2-syntx "{%....%} for each line is tremulous.

My first idea was to implement a python filter-plugin. But Ansible does
not search for plugins within the role's directory.

* Is there a way to include a block of Python-code into the template?

No

* Is there a way to directly call a Python-script or function located
relativly to the template?

You can try using `first_found` lookup to locate it, and `lines` lookup to call it.

* Any other solutions?

You can also implement that computation as a custom module, and call it before calling template

a filter_plugins (at play or role level) directory should enable you
to load custom plugins.

>* Is there a way to directly call a Python-script or function located
>relativly to the template?

You can try using `first_found` lookup to locate it, and `lines` lookup to call it.

I'll try this.

>* Any other solutions?

You can also implement that computation as a custom module, and call it before calling template

For me this sound wrong, because the module will be carried out on the
remote node, while there is nothing to do – except computing some
already known data.

You can always call it with local_action, then it won't be transferred to the remote host.