Ansible: Get number of hosts in group

Question:

I’m trying to get the number of hosts of a certain group.

Imagine an inventory file like this:

Now in my playbook I would like to get the number of hosts that are part of maingroup which would be 5 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.

At the moment I’m setting the variable manually which is far from ideal..

Answer:

Leave a Reply