# groupname as variable?

**URL:** <https://forum.ansible.com/t/groupname-as-variable/16732>\
**Category:** Ansible Project\
**Created:** [November 10, 2014, 7:16am UTC](https://forum.ansible.com/t/groupname-as-variable/16732 "2014-11-10T07:16:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![linbo\_liao](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@linbo\_liao](https://forum.ansible.com/u/linbo_liao)\
**Post date:** [November 10, 2014, 7:16am UTC](https://forum.ansible.com/t/groupname-as-variable/16732/1 "2014-11-10T07:16:23Z")

</div>

Hi,

I want set multi-cluster but don’t want defined multi-inventory files. Is there any solution for looping a variable defined for a group?

For example, inventory file is

[idc1-master]  
host1 cluster-node=idc1-node

[idc1-node]  
host2  
host3

[idc2-master]  
hosti cluster-node=idc2-node

[idc2-node]  
hostj  
hostk

playbook file

- name: add nodes  
debug: msg={{item}}  
with\_items:  
cluster-node  
tags:
- add-node

But this doesn’t work, variable cluster-node now is a string not a group. Any help?

Thanks,  
Linbo

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 10, 2014, 1:34pm UTC](https://forum.ansible.com/t/groupname-as-variable/16732/2 "2014-11-10T13:34:50Z")

</div>

The variable there is defined for a host, not a group, also avoid using dashes (-) in variable names.

It is not clear what you are trying to do here, do you want to add all hosts of a cluster? just the master nodes?

​

---

<div class="post-metadata">

**Author:** ![linbo\_liao](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@linbo\_liao](https://forum.ansible.com/u/linbo_liao)\
**Post date:** [November 12, 2014, 12:31pm UTC](https://forum.ansible.com/t/groupname-as-variable/16732/3 "2014-11-12T12:31:03Z")

</div>

Yes, want to add all nodes from cluster-node group to cluster.

在 2014年11月10日星期一UTC+8下午9时34分53秒，Brian Coca写道：

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 12, 2014, 1:56pm UTC](https://forum.ansible.com/t/groupname-as-variable/16732/4 "2014-11-12T13:56:21Z")

</div>

I believe this is what you want:

in the inventory:  
[idc1\_node]  
…

in the play:

- name: add nodes  
debug: msg={{item}}  
with\_items: groups[‘idc1\_node’]  
tags:

- add-node

---

<div class="post-metadata">

**Author:** ![linbo\_liao](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@linbo\_liao](https://forum.ansible.com/u/linbo_liao)\
**Post date:** [November 13, 2014, 2:16am UTC](https://forum.ansible.com/t/groupname-as-variable/16732/5 "2014-11-13T02:16:59Z")

</div>

Thanks.

I want setup multi cluster instance. So in the play I can’t specify the detailed cluster node groups, what I can do is use variable to cluster node groups.

For example,

[idc1\_master]  
host1 cluster\_node=idc1\_node

[idc1\_node]  
host2  
host3

[idc2\_master]  
hosti cluster\_node=idc2\_node

[idc2\_node]  
hostj  
hostk

idc1-master requires idc1\_node groups, idc2\_master requires idc2\_node groups. If playbook use idc1\_node, idc2 cluster will use idc1\_node, which is not correct.

在 2014年11月12日星期三UTC+8下午9时56分28秒，Brian Coca写道：

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 13, 2014, 2:09pm UTC](https://forum.ansible.com/t/groupname-as-variable/16732/6 "2014-11-13T14:09:35Z")

</div>

I’m not sure I understand the problem, you want to create the clusters but are not able to specify them?

---

<div class="post-metadata">

**Author:** ![linbo\_liao](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@linbo\_liao](https://forum.ansible.com/u/linbo_liao)\
**Post date:** [November 17, 2014, 6:35am UTC](https://forum.ansible.com/t/groupname-as-variable/16732/7 "2014-11-17T06:35:16Z")

</div>

Yes, different clusters require different node groups.

在 2014年11月13日星期四UTC+8下午10时09分39秒，Brian Coca写道：

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 17, 2014, 1:10pm UTC](https://forum.ansible.com/t/groupname-as-variable/16732/8 "2014-11-17T13:10:24Z")

</div>

ok, but you are just defining the master node in one host, you probably want group\_vars:

[idc1\_node:vars]  
cluster\_master=host1

[idc1\_node]  
host1  
host2  
host3
