# Using the magic variable groups

**URL:** <https://forum.ansible.com/t/using-the-magic-variable-groups/31009>\
**Category:** Ansible Project\
**Created:** [August 9, 2019, 3:11pm UTC](https://forum.ansible.com/t/using-the-magic-variable-groups/31009 "2019-08-09T15:11:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harrymosco](https://avatars.discourse-cdn.com/v4/letter/h/8c91f0/32.png) [@Harrymosco](https://forum.ansible.com/u/Harrymosco)\
**Post date:** [August 9, 2019, 3:11pm UTC](https://forum.ansible.com/t/using-the-magic-variable-groups/31009/1 "2019-08-09T15:11:41Z")

</div>

Can I use a variable to insert inventory group when using groups; e.g. groups[‘$groupname’]?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 13, 2019, 6:45pm UTC](https://forum.ansible.com/t/using-the-magic-variable-groups/31009/2 "2019-08-13T18:45:32Z")

</div>

no, that is what group\_by action is for, that variable is a copy and  
modifications won't persist

---

<div class="post-metadata">

**Author:** ![Harrymosco](https://avatars.discourse-cdn.com/v4/letter/h/8c91f0/32.png) [@Harrymosco](https://forum.ansible.com/u/Harrymosco)\
**Post date:** [August 14, 2019, 6:24pm UTC](https://forum.ansible.com/t/using-the-magic-variable-groups/31009/3 "2019-08-14T18:24:06Z")

</div>

Thank you Brian.

I have looked at “group\_by” and I am still trying to figure out how to use it in my case.

Maybe you can help me with how to make the piece of code below to be more generically used for any \<group\_name\>.  
I am using it in a template, to build a list of IP of hosts in a group so that I can use them to configure a cluster of servers.

Or if there is a better way of achieving the same purpose, please let me know.

{%for host in groups[‘\<group\_name\>’]%}“{{hostvars[host][‘ansible\_default\_ipv4’][‘address’]}}”{% if not loop.last %},{% endif %}{% endfor %}

I want to achieve having a config line like below:  
#discovery.zen.ping.unicast.hosts: [“IP\_Node1”,“\< IP\_Node2 \>”,“\<IP\_Node3\>"]

where there are 3 hosts in the \<group\_name\>.

Thanks.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 14, 2019, 9:14pm UTC](https://forum.ansible.com/t/using-the-magic-variable-groups/31009/4 "2019-08-14T21:14:30Z")

</div>

Lets say you have a variable myvar that contain the group name you can do groups[myvar]

---

<div class="post-metadata">

**Author:** ![Harrymosco](https://avatars.discourse-cdn.com/v4/letter/h/8c91f0/32.png) [@Harrymosco](https://forum.ansible.com/u/Harrymosco)\
**Post date:** [August 16, 2019, 12:20am UTC](https://forum.ansible.com/t/using-the-magic-variable-groups/31009/5 "2019-08-16T00:20:50Z")

</div>

Thanks Kai Stian,

Absolutely what I want. Fantastic!!

All I am doing now is use “–extra-vars” and assign any \<group\_name\> I want to configure to the variable, and that’s it.  
The code does not need to be modified anymore for different \<group\_name\>s.

Appreciated!!

-Harrymosco.
