# using {{item}} inside template

**URL:** <https://forum.ansible.com/t/using-item-inside-template/12967>\
**Category:** Ansible Project\
**Created:** [June 15, 2013, 3:34pm UTC](https://forum.ansible.com/t/using-item-inside-template/12967 "2013-06-15T15:34:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![javawebdeveloper](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@javawebdeveloper](https://forum.ansible.com/u/javawebdeveloper)\
**Post date:** [June 15, 2013, 3:34pm UTC](https://forum.ansible.com/t/using-item-inside-template/12967/1 "2013-06-15T15:34:03Z")

</div>

Hello,  
I have a template statement:

- name: Setup apache config for site  
template: src=generic\_site.j2 dest=/etc/apache2/sites-available/{{ item }} group=root owner=root  
with\_items: ${sites}

#where sites are: sites: [website1, website2]

This allows me to create the 2 site files /etc/apache2/sites-available/website1 and /etc/apache2/sites-available/website2

What I would really like to do is to be able to use the {{ item }} inside the generic\_site.j2 for the configuration of the file.

Like this:

\<VirtualHost \*:80\>  
ServerName {{ item }}  
ServerAlias {{ item }}

DocumentRoot /var/www/{{ item }}

Options FollowSymLinks AllowOverride None

\<Directory “/var/www/{{ item }}”\>  
Options Indexes FollowSymLinks MultiViews  
AllowOverride None  
Order allow,deny  
allow from all

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/  
\<Directory “/usr/lib/cgi-bin”\>  
AllowOverride None  
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch  
Order allow,deny  
Allow from all

ErrorLog ${APACHE\_LOG\_DIR}/{{ item }}/error.log

# Possible values include: debug, info, notice, warn, error, crit,

# alert, emerg.

LogLevel warn

CustomLog ${APACHE\_LOG\_DIR}/{{ item }}/access.log combined

But when I do this, the values are all empty. Any help would be greatly appreciated!!

Thanks,  
Brent

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 15, 2013, 4:22pm UTC](https://forum.ansible.com/t/using-item-inside-template/12967/2 "2013-06-15T16:22:38Z")

</div>

Are you using 1.2?

If so, you can just say:

with\_items: variable

No need for the ${variable} stuff

I’m pretty sure the variable “item” is in scope for the template too but I’ll have to test on my end.

Also, when posting a question/bug, always helps to detail what version of Ansible you are using! Thanks!

---

<div class="post-metadata">

**Author:** ![Oliver\_Heinz](https://avatars.discourse-cdn.com/v4/letter/o/db5fbb/32.png) [@Oliver\_Heinz](https://forum.ansible.com/u/Oliver_Heinz)\
**Post date:** [June 20, 2013, 1:05pm UTC](https://forum.ansible.com/t/using-item-inside-template/12967/3 "2013-06-20T13:05:15Z")

</div>

I did encounter the same problem with todays 1.3 build, I cannot access item (or it’s attributes in my case) in the template. I would be great if this worked.

Oliver

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 20, 2013, 3:58pm UTC](https://forum.ansible.com/t/using-item-inside-template/12967/4 "2013-06-20T15:58:22Z")

</div>

Ok, please make sure there’s a bug filed in github. This should be super easy to add.

---

<div class="post-metadata">

**Author:** ![Oliver\_Heinz](https://avatars.discourse-cdn.com/v4/letter/o/db5fbb/32.png) [@Oliver\_Heinz](https://forum.ansible.com/u/Oliver_Heinz)\
**Post date:** [June 21, 2013, 8:41am UTC](https://forum.ansible.com/t/using-item-inside-template/12967/5 "2013-06-21T08:41:32Z")

</div>

The problem seems to have vanished with todays build. Works now as expected.  
Should have tried again before updating, now I’m a bit puzzled. So you either fixed it already or I was mistaken and have to blame it upon the hot weather yesterday.

thanks anyway,  
Oliver

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 22, 2013, 5:37pm UTC](https://forum.ansible.com/t/using-item-inside-template/12967/6 "2013-06-22T17:37:22Z")

</div>

Yep, I think it’s more likely it always worked, because I was starting at the spot where the “item” variable was added to the template variables recently and was pretty sure it was done at least several months ago.
