本文介绍了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:获取组中的主机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 13:31