inventory: Add create_groups option

This adds the create_groups option to the inventory, which allows to
control the creation of groups from labels on VirtualMachines. By
default it is disabled.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
Felix Matouschek
2023-09-05 14:48:12 +02:00
parent 100bcee082
commit e33b479326
5 changed files with 33 additions and 16 deletions

View File

@@ -0,0 +1,2 @@
minor_changes:
- "inventory: Allow to control creation of additional groups"

View File

@@ -112,6 +112,11 @@ options:
- Enable the use of services to establish an SSH connection to the VirtualMachine. - Enable the use of services to establish an SSH connection to the VirtualMachine.
type: bool type: bool
default: True default: True
create_groups:
description:
- Enable the creation of groups from labels on VirtualMachines.
type: bool
default: False
api_version: api_version:
description: description:
- Specify the used KubeVirt API version. - Specify the used KubeVirt API version.
@@ -211,6 +216,7 @@ class GetVmiOptions:
network_name: Optional[str] = None network_name: Optional[str] = None
kube_secondary_dns: Optional[bool] = None kube_secondary_dns: Optional[bool] = None
use_service: Optional[bool] = None use_service: Optional[bool] = None
create_groups: Optional[bool] = None
base_domain: Optional[str] = None base_domain: Optional[str] = None
host_format: Optional[str] = None host_format: Optional[str] = None
@@ -222,6 +228,8 @@ class GetVmiOptions:
self.kube_secondary_dns = False self.kube_secondary_dns = False
if self.use_service is None: if self.use_service is None:
self.use_service = True self.use_service = True
if self.create_groups is None:
self.create_groups = False
if self.host_format is None: if self.host_format is None:
self.host_format = "{namespace}-{name}" self.host_format = "{namespace}-{name}"
@@ -374,6 +382,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
connection.get("network_name", connection.get("interface_name")), connection.get("network_name", connection.get("interface_name")),
connection.get("kube_secondary_dns"), connection.get("kube_secondary_dns"),
connection.get("use_service"), connection.get("use_service"),
connection.get("create_groups"),
connection.get("base_domain", self.get_cluster_domain(client)), connection.get("base_domain", self.get_cluster_domain(client)),
self.host_format, self.host_format,
) )
@@ -473,30 +482,33 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
name=vmi.metadata.name, name=vmi.metadata.name,
uid=vmi.metadata.uid, uid=vmi.metadata.uid,
) )
vmi_groups = []
vmi_annotations = ( vmi_annotations = (
{} {}
if not vmi.metadata.annotations if not vmi.metadata.annotations
else self.__resource_field_to_dict(vmi.metadata.annotations) else self.__resource_field_to_dict(vmi.metadata.annotations)
) )
vmi_labels = (
{}
if not vmi.metadata.labels
else self.__resource_field_to_dict(vmi.metadata.labels)
)
if vmi.metadata.labels: # Add vmi to the namespace group
# create a group for each label_value
for key, value in vmi.metadata.labels:
group_name = f"label_{key}_{value}"
group_name = self._sanitize_group_name(group_name)
if group_name not in vmi_groups:
vmi_groups.append(group_name)
self.inventory.add_group(group_name)
vmi_labels = self.__resource_field_to_dict(vmi.metadata.labels)
else:
vmi_labels = {}
# Add vmi to the namespace group, and to each label_value group
self.inventory.add_host(vmi_name) self.inventory.add_host(vmi_name)
self.inventory.add_child(namespace_group, vmi_name) self.inventory.add_child(namespace_group, vmi_name)
for group in vmi_groups:
self.inventory.add_child(group, vmi_name) # Create label groups and add vmi to it if enabled
if vmi.metadata.labels and opts.create_groups:
# Create a group for each label_value
vmi_groups = []
for key, value in vmi.metadata.labels:
group_name = self._sanitize_group_name(f"label_{key}_{value}")
if group_name not in vmi_groups:
vmi_groups.append(group_name)
# Add vmi to each label_value group
for group in vmi_groups:
self.inventory.add_group(group)
self.inventory.add_child(group, vmi_name)
# Set up the connection # Set up the connection
self.inventory.set_variable(vmi_name, "ansible_connection", "ssh") self.inventory.set_variable(vmi_name, "ansible_connection", "ssh")

View File

@@ -2,3 +2,4 @@ plugin: kubevirt.core.kubevirt
connections: connections:
- namespaces: - namespaces:
- default - default
create_groups: yes

View File

@@ -2,4 +2,5 @@ plugin: kubevirt.core.kubevirt
connections: connections:
- namespaces: - namespaces:
- default - default
create_groups: yes
label_selector: app=test label_selector: app=test

View File

@@ -2,4 +2,5 @@ plugin: kubevirt.core.kubevirt
connections: connections:
- namespaces: - namespaces:
- default - default
create_groups: yes
network_name: bridge-network network_name: bridge-network