Use of host and group variables in hosts patterns

722 views
Skip to first unread message

Petros Moisiadis

unread,
Apr 28, 2014, 11:32:34 AM4/28/14
to ansible...@googlegroups.com
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.

Brian Coca

unread,
Apr 28, 2014, 12:03:26 PM4/28/14
to ansible...@googlegroups.com
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).

Petros Moisiadis

unread,
Apr 28, 2014, 1:02:19 PM4/28/14
to ansible...@googlegroups.com
On 04/28/14 19:03, Brian Coca wrote:
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).
--
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/CADn%2BHsz-UGtZ7aT5am6Aa5QxDkpHdxL6h4FMi2FuGNewn1b8_A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

I actually got to workaround this by making the assumption that the first node in the "mycluster.example.org" group is the master node. So, the following hosts patterns work:

# match master node
- hosts: "{{ target + '_nodes' }}[0]"


# match other nodes
- hosts: "{{ target + '_nodes' }}:!{{ target + '_nodes' }}[0]"


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?

Petros Moisiadis

unread,
Apr 28, 2014, 1:04:30 PM4/28/14
to ansible...@googlegroups.com

For more options, visit https://groups.google.com/d/optout.

Oops, I actually meant "mycluster.example.org_nodes" group.

Brian Coca

unread,
Apr 28, 2014, 2:37:11 PM4/28/14
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages