# how to create many templates with an increasing variable (counter) in each?

**URL:** <https://forum.ansible.com/t/how-to-create-many-templates-with-an-increasing-variable-counter-in-each/17917>\
**Category:** Ansible Project\
**Created:** [April 7, 2015, 9:08am UTC](https://forum.ansible.com/t/how-to-create-many-templates-with-an-increasing-variable-counter-in-each/17917 "2015-04-07T09:08:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![vovansystems](https://avatars.discourse-cdn.com/v4/letter/v/aeb1de/32.png) [@vovansystems](https://forum.ansible.com/u/vovansystems)\
**Post date:** [April 7, 2015, 9:08am UTC](https://forum.ansible.com/t/how-to-create-many-templates-with-an-increasing-variable-counter-in-each/17917/1 "2015-04-07T09:08:03Z")

</div>

Hi,

I need to create configuration files for php5-fpm and nginx for many domains. Each php5-fpm’s worker should run on separate port starting from 127.0.0.1:9001 and so on.

I started with the following configuration:

- role: webserver

sites:

- name: user  
site:
- [test.com](http://test.com)
- [test.org](http://test.org)
- name: mike  
site:
- [domain.com](http://domain.com)
- [domain.org](http://domain.org)

tasks:

- name: create nginx-domain.conf  
template: src=nginx-domain.conf.j2 dest=/etc/nginx/conf.d/{{ item.1 }}.conf backup=yes  
with\_subelements:

- sites

- site  
notify: Reload nginx

- name: create php5-fpm-domain.conf  
template: src=php5-fpm-domain.conf.j2 dest=/etc/php5/fpm/pool.d/{{ item.1 }}.conf backup=yes  
with\_subelements:

- sites

- site  
notify: Reload php

nginx-domain.conf.j2:

server {

listen 80;  
server\_name {{ item.1 }};  
error\_log /home/{{ item.0.name }}/logs/{{ item.1 }}.error.log error;  
access\_log /home/{{ item.0.name }}/logs/{{ item.1 }}.access.log combined buffer=16k flush=1m;

root /www/{{ item.0.name }}/domains/{{ item.1 }}/public\_html/;  
set $sock 127.0.0.1:{{ counter }}

include fastcgi\_params;  
fastcgi\_index index.php;  
fastcgi\_intercept\_errors on;  
fastcgi\_param DOCUMENT\_ROOT /public\_html;  
fastcgi\_param SCRIPT\_FILENAME /public\_html$fastcgi\_script\_name;

index index.html index.php;

location / {  
try\_files $uri $uri/ /index.php$is\_args$args;  
}

location ~\* .php$ {  
try\_files $fastcgi\_script\_name =404;  
fastcgi\_pass $sock;  
}

}

so, i need to increase {{ counter }} starting from 9001 in a template for every domain to use it in line ‘set $sock 127.0.0.1:{{ counter }}’, but i don’t know how to do that. i didn’t find anything useful via google/stackoverflow.  
Maybe you can give me a hint or even some examples in which way to dig?
