问题
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