On 06/24/14 17:48, Claus Strommer wrote:
> Hi there,
>
> I have a play with a role that creates a load balancer template.  I've
> successfully tested it in my dev environment, using
>
>
> {% for host in groups['dev-web'] %}
>     server {{ host }} {{ host }}:80
> {% endfor %}
>
>
> the 'dev-web' group is defined in my inventory, as well as
> 'stage-web', 'prod-web', etc.  What I'd like to do is change this
> template so that it can be used in any environment.  I am already
> passing a parameter upon calling the play,  --extra-vars
> "deploy_env=dev".  In the play itself I can use this variable to
> determine on which balancer the template gets applied, by using
> something like this:
>
>
> - hosts: [ '{{ deploy_env }}-bal' ]
>   serial: 1
>   vars_files:
>   - /var/lib/ansible/vars/{{ deploy_env }}.yml
>   roles:
>   - role: "haproxy-webbal"
>
>
> However, if I try something similar in the template, e.g.
>
>
> {% for host in groups['{{ deploy_env }}-web'] %}
>     server {{ host }} {{ host }}:80
> {% endfor %}
>
> I get this error:
>
>
> fatal: [stage-webbal-06.aws.primal.com
> <http://stage-webbal-06.aws.primal.com>] => {'msg': "One or more
> undefined variables: 'dict object' has no attribute '{{ deploy_env
> }}-varnish'", 'failed': True}
>
>
> It seems that {{ deploy_env }} is not being expanded, and it's being
> taken as a literal.  Is this because it's within the {% %} section? 
> If so, then what is the correct syntax for doing this?  Alternatively,
> is there a cleaner way to approach this?
>

Access your var inside the "{% %}" block without a "{{ }}" surrounding
and use string concatenation like this:

{% for host in groups[deploy_env + '-web'] %}
    server {{ host }} {{ host }}:80
{% endfor %}


>
>
> -- 
> 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 ansible-project+unsubscr...@googlegroups.com
> <mailto:ansible-project+unsubscr...@googlegroups.com>.
> To post to this group, send email to ansible-project@googlegroups.com
> <mailto:ansible-project@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CABkDv%2B5o4bLiUiNgr-UxP9P8hWpe0SxkhPvhb-T25VrErrMvkw%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CABkDv%2B5o4bLiUiNgr-UxP9P8hWpe0SxkhPvhb-T25VrErrMvkw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/53A99512.1090004%40yahoo.gr.
For more options, visit https://groups.google.com/d/optout.

Reply via email to