template with groups as a variable

54 views
Skip to first unread message

Edgardo Vega

unread,
Nov 12, 2015, 7:50:18 AM11/12/15
to Ansible Project
I am trying to do the following where I index groups by a variable instead of hardcoding it. Is that possible somehow?

{% for host in groups['{{VAR}}'] %}
 STUFF
{% endfor %}


Thanks in advance,

Edgardo


Matt Martz

unread,
Nov 12, 2015, 7:59:20 AM11/12/15
to ansible...@googlegroups.com
You can't nest jinja2 delimiters.  You just use bare variable names inside such as:

{% for host in groups[VAR] %}

--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1d72aea3-f95f-45dc-99c5-f60f07fb51a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Edgardo Vega

unread,
Nov 12, 2015, 8:00:24 AM11/12/15
to Ansible Project
I figured it out. You can just do the following and it will work.

{% for host in groups[VAR] %}

Edgardo Vega

unread,
Nov 12, 2015, 8:44:59 AM11/12/15
to Ansible Project
Thanks Matt! is there way to do this in a playbook. The first one works the second one doesn't.

  - debug: msg="hey {{ item }}"
    with_items: groups['tag_env_prod']

  - debug: msg="hey {{ item }}"
    with_items: groups['{{group_name}}']

On Thursday, November 12, 2015 at 7:59:20 AM UTC-5, Matt Martz wrote:
You can't nest jinja2 delimiters.  You just use bare variable names inside such as:

{% for host in groups[VAR] %}

On Thursday, November 12, 2015, Edgardo Vega <edgard...@gmail.com> wrote:
I am trying to do the following where I index groups by a variable instead of hardcoding it. Is that possible somehow?

{% for host in groups['{{VAR}}'] %}
 STUFF
{% endfor %}


Thanks in advance,

Edgardo


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Brian Coca

unread,
Nov 12, 2015, 11:08:16 AM11/12/15
to Ansible Project
- debug: msg="hey {{ item }}"
with_items: "{{groups[group_name]}}"

On Thu, Nov 12, 2015 at 5:44 AM, Edgardo Vega <edgard...@gmail.com> wrote:
> Thanks Matt! is there way to do this in a playbook. The first one works the
> second one doesn't.
>
> - debug: msg="hey {{ item }}"
> with_items: groups['tag_env_prod']
>
> - debug: msg="hey {{ item }}"
> with_items: groups['{{group_name}}']
>
> On Thursday, November 12, 2015 at 7:59:20 AM UTC-5, Matt Martz wrote:
>>
>> You can't nest jinja2 delimiters. You just use bare variable names inside
>> such as:
>>
>> {% for host in groups[VAR] %}
>>
>> On Thursday, November 12, 2015, Edgardo Vega <edgard...@gmail.com> wrote:
>>>
>>> I am trying to do the following where I index groups by a variable
>>> instead of hardcoding it. Is that possible somehow?
>>>
>>> {% for host in groups['{{VAR}}'] %}
>>> STUFF
>>> {% endfor %}
>>>
>>>
>>> Thanks in advance,
>>>
>>> Edgardo
>>>
>>>
>>> --
>>> 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-proje...@googlegroups.com.
>>> To post to this group, send email to ansible...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/ansible-project/1d72aea3-f95f-45dc-99c5-f60f07fb51a0%40googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> Matt Martz
>> @sivel
>> sivel.net
>>
> --
> 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-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/c970b09c-cb50-4cad-b9cc-44161b152009%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca
Reply all
Reply to author
Forward
0 new messages