# Template module doesn't work with any kind of loop

**URL:** <https://forum.ansible.com/t/template-module-doesnt-work-with-any-kind-of-loop/37705>\
**Category:** Ansible Project\
**Created:** [February 28, 2023, 10:22pm UTC](https://forum.ansible.com/t/template-module-doesnt-work-with-any-kind-of-loop/37705 "2023-02-28T22:22:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darkhan](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Darkhan](https://forum.ansible.com/u/Darkhan)\
**Post date:** [February 28, 2023, 10:22pm UTC](https://forum.ansible.com/t/template-module-doesnt-work-with-any-kind-of-loop/37705/1 "2023-02-28T22:22:15Z")

</div>

Hello,

I’m having a textbook task (taken straight out from the documentation examples) using template and with\_filetree:

- name: axis pipeline  
template:  
src: “{{ item.src }}”  
dest: “{{ logstash\_home }}/{{ item.path | splitext | first }}”

with\_filetree: …/templates/logstash/pipelines/axis

I get this error:  
AnsibleFilterError: dict2items requires a dictionary, got \<class ‘ansible.template.AnsibleUndefined’\> instead.

I get the same error with with\_fileglob.

I have also tried different types of loops, nothing works. Everything I’ve tried works fine with the copy module.

So what is so special about the template module? It should work similarly with copy in terms of copying files, iterating over filesystem paths etc.  
I’m using ansible 2.9.

---

<div class="post-metadata">

**Author:** ![Darkhan](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Darkhan](https://forum.ansible.com/u/Darkhan)\
**Post date:** [February 28, 2023, 10:46pm UTC](https://forum.ansible.com/t/template-module-doesnt-work-with-any-kind-of-loop/37705/2 "2023-02-28T22:46:17Z")

</div>

Seems this is caused by the path in the src. You can’t put anything there except the file name, and it will look in playbooks/templates/filename. If i try to put a path, it throws that error.  
This behavior happens only with loops though.

Without loops, I can put a full or relative path in the src and it works.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 1, 2023, 1:52pm UTC](https://forum.ansible.com/t/template-module-doesnt-work-with-any-kind-of-loop/37705/3 "2023-03-01T13:52:14Z")

</div>

Current docs for with\_filetree show this example:

- name: Template files (explicitly skip directories in order to use the ‘src’ attribute)  
template:  
src: ‘{{ item.src }}’  
dest: /web/{{ item.path }}  
mode: ‘{{ item.mode }}’

with\_filetree: web/  
when: item.state == ‘file’

I suspect it’s the directories that are causing your problems.
