Hello,
I’m trying to figure out how to loop over a variable with with_items and pass it to the template module with a different name other than item.
The issue is that I don’t want to refer to {{ item }}, {{ item.host }} and etc in the jinja2 templates, because it’s a terrible variable name outside of the context of the task and makes it hard for people looking at just the templates to figure out what they’re supposed to do. I wasn’t able to figure out a generic way to set variables inside tasks, so in 1.3, I was hacking this behavior with with_items + include, but it was deprecated in 1.4.
This is what I had:
main.yml:
- include: conf.yml vhost=${item}
with_items: ${vhosts}
And then in conf.yml:
- name: set up nginx conf for vhost
template: src={{ vhost.template | default(“vhost.conf.j2”) }} dest=“{{ nginx_conf_path }}/{{ vhost.host }}{% if vhost.suffix is defined %}_{{ vhost.suffix }}{% endif %}.conf” backup=true
notify: restart nginx
tags: nginx
The templates themselves refer to the vhost variable quite often.
I can understand why this was deprecated, it was a strange thing that produced strange results that were rather inconsistent (originally I had with_items: vhosts up in there, as it should be, but it stopped working after an upgrade. I was also using the old, non-j2 syntax vhost=${item} because vhost={{item}} didn’t seem to work and vhost=item just passed the string “item” to conf.yml).
My question is, is there any better way to do what I’m trying to do (and compatible with 1.4+, of course)? Sorry if I missed it, I went through all of http://www.ansibleworks.com/docs/playbooks_loops.html, but it seems I can’t get away from having the loop variable called “item.”
Thanks.