title: Ansible: Get number of hosts in group
body: <p>I'm trying to get the number of hosts of a certain group.  </p>
  
  <p>Imagine an inventory file like this:</p>
  
  <pre><code>[maingroup]
  server-[01:05]
  </code></pre>
  
  <p>Now in my playbook I would like to get the number of hosts that are part of <code>maingroup</code> which would be <code>5</code> in this case and store that in a variable which is supposed to be used in a template in one of the playbook's tasks. </p>
  
  <p>At the moment I'm setting the variable manually which is far from ideal..  </p>
  
  <pre><code>vars:
    HOST_COUNT: 5
  </code></pre>