Jinja template as variable value

299 views
Skip to first unread message

Mario S

unread,
May 6, 2021, 4:26:03 AM5/6/21
to Ansible Project

Hello

I'm trying to use jinja template as value for ansible.builtin.lineinfile module 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?

Stefan Hornburg (Racke)

unread,
May 6, 2021, 5:13:28 AM5/6/21
to ansible...@googlegroups.com
On 5/6/21 10:26 AM, Mario S wrote:
> 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?

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

Regards
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
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/a5a95201-3212-4756-b222-4c430766de6bn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/a5a95201-3212-4756-b222-4c430766de6bn%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Stefan Hornburg (Racke)

unread,
May 7, 2021, 2:42:29 AM5/7/21
to ansible...@googlegroups.com
Hello,

in this case -vvv doesn't help.

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

Mario S

unread,
May 7, 2021, 4:13:49 AM5/7/21
to Ansible Project
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.

Brian Coca

unread,
May 7, 2021, 10:21:28 AM5/7/21
to Ansible Project
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}) }}
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/dd582874-331c-4f9c-8b7b-7ab2bd3e84fdn%40googlegroups.com.



--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages