# Jinja template as variable value

**URL:** <https://forum.ansible.com/t/jinja-template-as-variable-value/35122>\
**Category:** Ansible Project\
**Created:** [May 6, 2021, 8:26am UTC](https://forum.ansible.com/t/jinja-template-as-variable-value/35122 "2021-05-06T08:26:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_S](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@Mario\_S](https://forum.ansible.com/u/Mario_S)\
**Post date:** [May 6, 2021, 8:26am UTC](https://forum.ansible.com/t/jinja-template-as-variable-value/35122/1 "2021-05-06T08:26:03Z")

</div>

Hello

I’m trying to use jinja template as value for [ansible.builtin.lineinfile module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html) in role task.

named  
├── tasks  
│ ├── config.yml  
│ ├── main.yml  
├── templates  
│ ├── option.ji2  
│ ├── view.ji2

ansible.builtin.lineinfile:  
path: “{{ test\_dir }}{{ named\_config\_file[‘path’] }}”  
regex: “{{ item[‘key’] }}”  
line: “{% set option = item %}{% include ‘option.ji2’ %}”  
validate: named-checkconf %s  
loop: “{{ named\_options | dict2items }}”

Unfortunately ansible throws **TemplateNotFound** error:  
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: jinja2.exceptions.TemplateNotFound: option.ji2

Using relative or absolute path to template doesn’t help.  
templates/option.ji2  
…/templates/option.ji2  
/home/ansible/.ansible/roles/named/templates/option.ji2

Any ideas why ansible can’t find/use template option.ji2?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 6, 2021, 9:13am UTC](https://forum.ansible.com/t/jinja-template-as-variable-value/35122/2 "2021-05-06T09:13:04Z")

</div>

> Hello
> 
> I'm trying to use jinja template as value for ansible.builtin.lineinfile module  
> \<[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile\_module.html&gt](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html&gt); in role task.
> 
> named  
> ├── tasks  
> │ ├── config.yml  
> │ ├── main.yml  
> ├── templates  
> │ ├── option.ji2  
> │ ├── view.ji2
> 
> ansible.builtin.lineinfile:  
> path: "{{ test\_dir }}{{ named\_config\_file['path'] }}"  
> regex: "{{ item['key'] }}"  
> line: "{% set option = item %}{% include 'option.ji2' %}"  
> validate: named-checkconf %s  
> loop: "{{ named\_options | dict2items }}"
> 
> Unfortunately ansible throws \*TemplateNotFound\* error:  
> An exception occurred during task execution. To see the full traceback,

use -vvv. The error was:

> jinja2.exceptions.TemplateNotFound: option.ji2
> 
> Using relative or absolute path to template doesn't help.  
> templates/option.ji2  
> ../templates/option.ji2  
> /home/ansible/.ansible/roles/named/templates/option.ji2
> 
> Any ideas why ansible can't find/use template option.ji2?

I suggest to follow the advice to use -vvv and see which paths are considered by Ansible.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 7, 2021, 6:42am UTC](https://forum.ansible.com/t/jinja-template-as-variable-value/35122/3 "2021-05-07T06:42:02Z")

</div>

> > Hello
> > 
> > I'm trying to use jinja template as value for ansible.builtin.lineinfile module  
> > \<[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile\_module.html&gt](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html&gt); in role task.
> > 
> > named  
> > ├── tasks  
> > │ ├── config.yml  
> > │ ├── main.yml  
> > ├── templates  
> > │ ├── option.ji2  
> > │ ├── view.ji2
> > 
> > ansible.builtin.lineinfile:  
> > path: "{{ test\_dir }}{{ named\_config\_file['path'] }}"  
> > regex: "{{ item['key'] }}"  
> > line: "{% set option = item %}{% include 'option.ji2' %}"  
> > validate: named-checkconf %s  
> > loop: "{{ named\_options | dict2items }}"
> > 
> > Unfortunately ansible throws \*TemplateNotFound\* error:  
> > An exception occurred during task execution. To see the full traceback,
> 
> use -vvv. The error was:
> 
> > jinja2.exceptions.TemplateNotFound: option.ji2
> > 
> > Using relative or absolute path to template doesn't help.  
> > templates/option.ji2  
> > ../templates/option.ji2  
> > /home/ansible/.ansible/roles/named/templates/option.ji2
> > 
> > Any ideas why ansible can't find/use template option.ji2?
> 
> I suggest to follow the advice to use -vvv and see which paths are considered by Ansible.
> 
> Regards  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

Hello,

in this case -vvv doesn't help.

I found out with strace that Ansible looks into the current directory for  
the %include file.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

> > --  
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.  
> > To unsubscribe from this group and stop receiving emails from it, send

an email to

---

<div class="post-metadata">

**Author:** ![Mario\_S](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@Mario\_S](https://forum.ansible.com/u/Mario_S)\
**Post date:** [May 7, 2021, 8:13am UTC](https://forum.ansible.com/t/jinja-template-as-variable-value/35122/4 "2021-05-07T08:13:49Z")

</div>

Thank you for checking it out.  
So shouldn’t …/templates/option.ji2 work?

Anyways, I had to move on with the project and now template module is used instead of lineinfile, inside ji2 file include works as expected.

Not the most elegant solution but it does the job.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 7, 2021, 2:20pm UTC](https://forum.ansible.com/t/jinja-template-as-variable-value/35122/5 "2021-05-07T14:20:38Z")

</div>

This won't work as you expect  
line: "{% set option = item %}{% include 'option.ji2' %}"

do this instead

line: " {{lookup('template', 'option.j2', template\_vars={'option': item}) }}
