Q:

How to easily access a group of nodes in Ansible?

I have a single role that has slightly different nodes in it. I usually do the following tasks/main.yml:
- include: spark_master.yml
  when: spark_master|default(false)|bool

- include: spark_worker.yml
  when: spark_worker|default(false)|bool

And than in inventory file:
[spark]
192.168.172.101 spark_master=true spark_worker=false
192.168.172.102 spark_master=false spark_worker=true
192.168.172.103 spark_master=false spark_worker=true

However, if I need to access all of the workers in a template in some other role I do not have a good way of doing that.
I was thinking about having actual host group entries instead and that way I can reference workers like this:
iventory:
[spark-workers]
node1
node2

template in other role:
{{ groups['spark-workers'] | map('extract', hostvars, ['ansible_eth0', 'ipv4', 'address']) }}

Is there a better way of accessing a subgroup of nodes without actually using different groups in the inventory file?

A:

I'm sorry, as my answer contradicts with "no separate groups" requirement, but have you considered using children groups?
So in your case you'll have a 
[spark:children]
spark-master
spark-workers
[spark-master]
192.168.172.101
[spark-workers]
192.168.172.102
192.168.172.103

and then you can reference groups['spark-workers'] and groups['spark'] as you need.

