# Use of host and group variables in hosts patterns

**URL:** <https://forum.ansible.com/t/use-of-host-and-group-variables-in-hosts-patterns/15100>\
**Category:** Ansible Project\
**Created:** [April 28, 2014, 3:32pm UTC](https://forum.ansible.com/t/use-of-host-and-group-variables-in-hosts-patterns/15100 "2014-04-28T15:32:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [April 28, 2014, 3:32pm UTC](https://forum.ansible.com/t/use-of-host-and-group-variables-in-hosts-patterns/15100/1 "2014-04-28T15:32:34Z")

</div>

I am trying to use a host variable (actually a group variable) in a  
"hosts:" pattern in a playbook, but without success. Is this supposed to  
work?

The use case is this: I want to write a playbook that is invoked by  
passing an extra var named "target" in the command-line. The "target"  
actually denotes the name of a ganeti-based cluster of virtualization  
nodes, let's name it "mycluster.example.org". In the inventory there is  
a group named "mycluster.example.org", which has two children groups  
named "mycluster.example.org\_nodes" (for ganeti nodes) and  
"mycluster.example.org\_instances" (for ganeti instances):

[mycluster.example.org:children]  
mycluster.example.org\_nodes  
mycluster.example.org\_instances

[mycluster.example.org\_nodes]  
node1.mycluster.example.org  
node2.mycluster.example.org

The playbook has two plays. The first play must be run against the  
master node and the other must be run against all other nodes (except  
master). The master node is defined in a group variable of  
"mycluster.example.org" group. So, I am trying for the first play the  
following hosts pattern in the playbook, but it does not work:

- hosts: {{ hostvars[groups[target + '\_nodes'][0]]['master\_node'] }}

I am not sure, but it seems that hostvars/groups variables are not  
available for use in hosts patterns.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [April 28, 2014, 4:03pm UTC](https://forum.ansible.com/t/use-of-host-and-group-variables-in-hosts-patterns/15100/2 "2014-04-28T16:03:26Z")

</div>

it is not supposed to work at all. ​which host or group would it look at to set the hosts?

you might want to look at group\_by to do dynamic groups based on variables that can be used in subsequent plays (in same run).

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [April 28, 2014, 5:02pm UTC](https://forum.ansible.com/t/use-of-host-and-group-variables-in-hosts-patterns/15100/3 "2014-04-28T17:02:19Z")

</div>

I actually got to workaround this by making the assumption that the first node in the “[mycluster.example.org](http://mycluster.example.org)” group is the master node. So, the following hosts patterns work: Nevertheless, I think it would not hurt a feature addition to support host/group vars in the hosts patterns. It would not be something that we should suggest to users, but perhaps it would come handy in some scenarios that involve data-driven host matches. What do you think?

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [April 28, 2014, 5:04pm UTC](https://forum.ansible.com/t/use-of-host-and-group-variables-in-hosts-patterns/15100/4 "2014-04-28T17:04:30Z")

</div>

Oops, I actually meant “mycluster.example.org\_nodes” group.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [April 28, 2014, 6:37pm UTC](https://forum.ansible.com/t/use-of-host-and-group-variables-in-hosts-patterns/15100/5 "2014-04-28T18:37:11Z")

</div>

you used groups, which is allowed, what you cannot use is the ‘master\_node’ variable, and that will never work.

you can have a play with group\_by on ‘master\_node’, that creates groups for the other 2  
​
