Ansible: Get number of hosts in group

。_饼干妹妹 提交于 2019-12-05 13:05:06

问题


I'm trying to get the number of hosts of a certain group.

Imagine an inventory file like this:

[maingroup]
server-[01:05]

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

回答1:


  vars:
    HOST_COUNT: "{{ groups['maingroup'] | length }}"



回答2:


Also without explicit group name:

vars:
    HOST_COUNT: "{{ ansible_play_hosts | length }}"


来源:https://stackoverflow.com/questions/36310633/ansible-get-number-of-hosts-in-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!