# generating multiple files from one template

**URL:** <https://forum.ansible.com/t/generating-multiple-files-from-one-template/14660>\
**Category:** Ansible Project\
**Created:** [February 26, 2014, 3:08pm UTC](https://forum.ansible.com/t/generating-multiple-files-from-one-template/14660 "2014-02-26T15:08:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy\_Altepeter](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Andy\_Altepeter](https://forum.ansible.com/u/Andy_Altepeter)\
**Post date:** [February 26, 2014, 3:08pm UTC](https://forum.ansible.com/t/generating-multiple-files-from-one-template/14660/1 "2014-02-26T15:08:13Z")

</div>

Greetings,

I have a need to generate multiple configuration files on a single host from one template. Is this possible in ansible?

Let’s say I have a variable like:

vars:  
configs:

- port: 8080  
directory: /var/www/host1
- port: 8081  
directory: /var/www/host2

I need to generate a configuration file for each tuple in this list. The template needs to have access to each tuple in the list using a single name. In a python program it might look like:

for config in configs:  
generate\_template(config=config)

And the template then uses the variable name “config” to refer to the current tuple.

Is this possible with just ansible? I know I can use with\_items to iterate over the list, but does the template have access to the “current” tuple?

Thanks,  
Andy

---

<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:** [February 26, 2014, 3:25pm UTC](https://forum.ansible.com/t/generating-multiple-files-from-one-template/14660/2 "2014-02-26T15:25:31Z")

</div>

You can easily put variables in your template that the template can key off of.

Usage of things like “set\_fact” or parameterized role arguments may be relevant here.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [February 26, 2014, 4:16pm UTC](https://forum.ansible.com/t/generating-multiple-files-from-one-template/14660/3 "2014-02-26T16:16:31Z")

</div>

very easy:

- template: src=sametemplate dest=/var/www/{{item}}with\_items:
- host1
- host2  
and you can use ‘item’ as a key inside the template to write specific options to each.
