# passing var to a template in  a loop

**URL:** <https://forum.ansible.com/t/passing-var-to-a-template-in-a-loop/15878>\
**Category:** Ansible Developer\
**Created:** [August 6, 2014, 7:38pm UTC](https://forum.ansible.com/t/passing-var-to-a-template-in-a-loop/15878 "2014-08-06T19:38:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![avrman](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@avrman](https://forum.ansible.com/u/avrman)\
**Post date:** [August 6, 2014, 7:38pm UTC](https://forum.ansible.com/t/passing-var-to-a-template-in-a-loop/15878/1 "2014-08-06T19:38:59Z")

</div>

I want to use a template to copy a file to a destination with a unique name, on each copy.  
I also want to pass a unique value for a variable used in the jinja2 template file.  
How can I do this?

I know I can use with\_items to specify a list and thus change the destination file name on each copy,  
but how can I change the variable inside the template to a corresponding value for each file name ?

---

<div class="post-metadata">

**Author:** ![avrman](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@avrman](https://forum.ansible.com/u/avrman)\
**Post date:** [August 6, 2014, 9:39pm UTC](https://forum.ansible.com/t/passing-var-to-a-template-in-a-loop/15878/2 "2014-08-06T21:39:52Z")

</div>

To be specific, I want this file copied over multiple times with a unique name each time and I also want a variable within each copy to set to a distinct value.  
e.g. dest file-name = file1 with variable in template set to var1  
dest file-name = file2 with variable in template set to var2  
etc.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 6, 2014, 10:59pm UTC](https://forum.ansible.com/t/passing-var-to-a-template-in-a-loop/15878/3 "2014-08-06T22:59:26Z")

</div>

Hi,

This is more of a user level question, would you mind posting this to ansible-project instead?

We’ll probably have some follow up questions once posted there.

Thanks!
