Ansible:获取组中的主机数 [英] Ansible: Get number of hosts in group
本文介绍了Ansible:获取组中的主机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取某个组的主机数.
I'm trying to get the number of hosts of a certain group.
想象一下这样的清单文件:
Imagine an inventory file like this:
[maingroup]
server-[01:05]
现在在我的剧本中,我想获取属于maingroup
的主机的数量(在这种情况下为5
)并将其存储在应该用于以下模板之一的变量中:剧本的任务.
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..
vars:
HOST_COUNT: 5
推荐答案
vars:
HOST_COUNT: "{{ groups['maingroup'] | length }}"
这篇关于Ansible:获取组中的主机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文