How about renaming the variable at the top of your template:

    {% set vhost=item %}{# from 'with_items' in the calling task #}

This way you are clearly documenting where vhost is coming from.

K

Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



On 4 December 2013 03:38, Miguel Pontes <[email protected]> wrote:
> 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.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to