using {{item}} inside template

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

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!

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

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

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

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.