feat: Set ansible_connection to winrm for Windows hosts

This changes the inventory plugin so that it sets the ansible_connection
to winrm if it detected a Windows host. If it did not detect a Windows
host the ansible_connection is no longer set, so Ansible falls back to
its default value of ssh. The detection of SSH services for hosts using
winrm is disabled.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
Felix Matouschek
2024-04-15 14:28:57 +02:00
parent 7c5de4adf9
commit a3abcbedd4
4 changed files with 251 additions and 15 deletions

View File

@@ -202,10 +202,13 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import
K8SClient,
)
ANNOTATION_KUBEVIRT_IO_CLUSTER_PREFERENCE_NAME = "kubevirt.io/cluster-preference-name"
ANNOTATION_KUBEVIRT_IO_PREFERENCE_NAME = "kubevirt.io/preference-name"
ANNOTATION_VM_KUBEVIRT_IO_OS = "vm.kubevirt.io/os"
LABEL_KUBEVIRT_IO_DOMAIN = "kubevirt.io/domain"
TYPE_LOADBALANCER = "LoadBalancer"
TYPE_NODEPORT = "NodePort"
ID_MSWINDOWS = "mswindows"
class KubeVirtInventoryException(Exception):
@@ -318,6 +321,26 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
return None
@staticmethod
def is_windows(guest_os_info: Dict, annotations: Dict) -> bool:
if "id" in guest_os_info:
return guest_os_info["id"] == ID_MSWINDOWS
if ANNOTATION_KUBEVIRT_IO_CLUSTER_PREFERENCE_NAME in annotations:
return annotations[
ANNOTATION_KUBEVIRT_IO_CLUSTER_PREFERENCE_NAME
].startswith("windows")
if ANNOTATION_KUBEVIRT_IO_PREFERENCE_NAME in annotations:
return annotations[ANNOTATION_KUBEVIRT_IO_PREFERENCE_NAME].startswith(
"windows"
)
if ANNOTATION_VM_KUBEVIRT_IO_OS in annotations:
return annotations[ANNOTATION_VM_KUBEVIRT_IO_OS].startswith("windows")
return False
def __init__(self) -> None:
super().__init__()
self.host_format = None
@@ -523,16 +546,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
self.inventory.add_group(group)
self.inventory.add_child(group, vmi_name)
# Set up the connection
self.inventory.set_variable(vmi_name, "ansible_connection", "ssh")
self.set_ansible_host_and_port(
vmi,
vmi_name,
interface.ipAddress,
services.get(vmi.metadata.labels.get(LABEL_KUBEVIRT_IO_DOMAIN)),
opts,
)
# Add hostvars from metadata
self.inventory.set_variable(vmi_name, "object_type", "vmi")
self.inventory.set_variable(vmi_name, "labels", vmi_labels)
@@ -605,6 +618,23 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
self.inventory.set_variable(
vmi_name, "vmi_volume_status", vmi_volume_status
)
# Set up the connection
service = None
if self.is_windows(vmi_guest_os_info, vmi_annotations):
self.inventory.set_variable(vmi_name, "ansible_connection", "winrm")
else:
service = services.get(
vmi.metadata.labels.get(LABEL_KUBEVIRT_IO_DOMAIN)
)
self.set_ansible_host_and_port(
vmi,
vmi_name,
interface.ipAddress,
service,
opts,
)
self.set_composable_vars(vmi_name)
def set_composable_vars(self, vmi_name):