直接上列子吧:
比如有如下的一個hosts文件,我分別想獲取主機組master、node、cluster的主機數量
[master]
192.168.100.28 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='root' host_id=1
192.168.100.29 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='root' host_id=2
192.168.100.30 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='root' host_id=3
[node]
192.168.100.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='root' host_id=4
[cluster:children]
master
node
全局變量這樣定義:
Master_Count: "{{ groups['master'] | length }}"
Node_Count: "{{ groups['node'] | length }}"
Cluster_Count: "{{ groups['cluster'] | length }}"
得到結果分別是:3、1、4