Ansible: How to create empty inventory group in playbook?

你。 提交于 2021-02-10 14:59:43

问题


I use some roles with playbooks which setting up a dozen services across a cluster and use inventory groups to specify hosts for roles.

According to the environment I need to extract an inventory from ENV in playbook. In the common case most of the services aren't necessary, so there is no hosts to add to a corresponding group, but a group must exist.

In case of a static inventory it's pretty easy. But how to define an empty group in a playbook?


回答1:


I couldn't find how to create an empty inventory group in a playbook. I did however find how to pass an empty inventory group from Vagrant. It may be almost empty, for example it contains not a host name, but a host name template which will not match to any host.

For example:

services.each do |service|
   hosts = (ENV["SERVERS_#{service.upcase}"] || "").split(",").map{|i| cluster_nodes[i.to_i]}
   groups[service] = hosts
   groups[service] = ["stub-a:f"] if groups[service] == []
ansible.groups = groups


来源:https://stackoverflow.com/questions/45786564/ansible-how-to-create-empty-inventory-group-in-playbook

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