# Complex computation within jinja-templates in roles?

**URL:** <https://forum.ansible.com/t/complex-computation-within-jinja-templates-in-roles/17187>\
**Category:** Ansible Project\
**Created:** [January 5, 2015, 2:38pm UTC](https://forum.ansible.com/t/complex-computation-within-jinja-templates-in-roles/17187 "2015-01-05T14:38:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hartmut\_Goebel](https://avatars.discourse-cdn.com/v4/letter/h/db5fbb/32.png) [@Hartmut\_Goebel](https://forum.ansible.com/u/Hartmut_Goebel)\
**Post date:** [January 5, 2015, 2:38pm UTC](https://forum.ansible.com/t/complex-computation-within-jinja-templates-in-roles/17187/1 "2015-01-05T14:38:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [January 5, 2015, 3:05pm UTC](https://forum.ansible.com/t/complex-computation-within-jinja-templates-in-roles/17187/2 "2015-01-05T15:05:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 5, 2015, 4:08pm UTC](https://forum.ansible.com/t/complex-computation-within-jinja-templates-in-roles/17187/3 "2015-01-05T16:08:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hartmut\_Goebel](https://avatars.discourse-cdn.com/v4/letter/h/db5fbb/32.png) [@Hartmut\_Goebel](https://forum.ansible.com/u/Hartmut_Goebel)\
**Post date:** [January 5, 2015, 4:35pm UTC](https://forum.ansible.com/t/complex-computation-within-jinja-templates-in-roles/17187/4 "2015-01-05T16:35:22Z")

</div>

> > \>\* 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.

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [January 5, 2015, 4:38pm UTC](https://forum.ansible.com/t/complex-computation-within-jinja-templates-in-roles/17187/5 "2015-01-05T16:38:22Z")

</div>

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