Ansible – Number of hosts per group

There is a "hosts" file in which in the "db" group there are N number of hosts, you need to automatically set a variable in the playbook equal to the number of hosts in a particular group

hosts

[db]
db1 ansible_host=192.168.1.101
db2 ansible_host=192.168.1.102
db3 ansible_host=192.168.1.103
db4 ansible_host=192.168.1.104
...

 

example.yaml

---
- hosts: localhost
  roles:
    - db/example
  vars:
    DB_COUNT: "{{ groups['db'] | length }}"

Tagged: Tags

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments