title: How to easily access a group of nodes in Ansible?
body: <p>I have a single role that has slightly different nodes in it. I usually do the following tasks/main.yml:</p>
  
  <pre><code>- include: spark_master.yml
    when: spark_master|default(false)|bool
  
  - include: spark_worker.yml
    when: spark_worker|default(false)|bool
  </code></pre>
  
  <p>And than in inventory file:</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>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.</p>
  
  <p>I was thinking about having actual host group entries instead and that way I can reference workers like this:</p>
  
  <p>iventory:</p>
  
  <pre><code>[spark-workers]
  node1
  node2
  </code></pre>
  
  <p>template in other role:</p>
  
  <pre><code>{{ groups['spark-workers'] | map('extract', hostvars, ['ansible_eth0', 'ipv4', 'address']) }}
  </code></pre>
  
  <p>Is there a better way of accessing a subgroup of nodes without actually using different groups in the inventory file?</p>