Ansible: Get the number of hosts in a group

I am trying to get the number of hosts of a specific group.

Imagine an inventory file, for example:

[maingroup] server-[01:05] 

Now in my play I would like to get the number of hosts that are part of the maingroup , which in this case will be 5 , and save this in a variable that should be used in the template in one of the tasks in the book book.

At the moment, I am setting a variable manually, which is far from ideal.

 vars: HOST_COUNT: 5 
+7
ansible ansible-playbook
source share
1 answer
  vars: HOST_COUNT: "{{ groups['maingroup'] | length }}" 
+19
source share

All Articles