Skip to content

Commit

Permalink
feat: Set ansible_connection to winrm for Windows hosts
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
0xFelix committed Apr 9, 2024
1 parent 95d07ce commit 7720613
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions plugins/inventory/kubevirt.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,13 @@ class DynamicApiError(Exception):
)


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):
Expand Down Expand Up @@ -317,6 +321,26 @@ def get_port_from_service(service: Dict) -> Optional[str]:

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
Expand Down Expand Up @@ -522,16 +546,6 @@ def get_vmis_for_namespace(
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)
Expand Down Expand Up @@ -605,6 +619,22 @@ def get_vmis_for_namespace(
vmi_name, "vmi_volume_status", vmi_volume_status
)

# Set up the connection
is_windows = self.is_windows(vmi_guest_os_info, vmi_annotations)
if is_windows:
self.inventory.set_variable(vmi_name, "ansible_connection", "winrm")
self.set_ansible_host_and_port(
vmi,
vmi_name,
interface.ipAddress,
(
services.get(vmi.metadata.labels.get(LABEL_KUBEVIRT_IO_DOMAIN))
if not is_windows
else None
),
opts,
)

def get_ssh_services_for_namespace(self, client: K8SClient, namespace: str) -> Dict:
"""
get_ssh_services_for_namespace retrieves all services of a namespace exposing port 22/ssh.
Expand Down

0 comments on commit 7720613

Please sign in to comment.