feat(inventory): add default_win_ansible_connection option

It adds the option `default_win_ansible_connection` to allow users to
configure the default `ansible_connection` for Windows VMs detected by
the inventory plugin, instead of always hardcoding `winrm`. This enables
use of alternative connection plugins like `psrp` or `ssh` without
per-host overrides.

Defaults to `winrm` for backward compatibility.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Javier Cano Cano <jcanocan@redhat.com>
This commit is contained in:
Javier Cano Cano
2026-05-06 15:49:05 +02:00
parent 19cc611159
commit 42fd3fecfd
4 changed files with 139 additions and 31 deletions

View File

@@ -83,6 +83,21 @@ options:
- Append the base domain of the cluster to host names constructed from SSH C(Services) of type C(NodePort).
type: bool
default: False
default_win_ansible_connection:
description:
- Set the default value of C(ansible_connection) when a Windows virtual machines is detected.
- Common values include V(winrm), V(ansible.builtin.winrm), V(psrp), V(ansible.builtin.psrp), V(ssh), and V(ansible.builtin.ssh).
- When set to V(ssh) or V(ansible.builtin.ssh), the service port lookup uses SSH port (22) instead of WinRM/PSRP ports (5986/5985).
type: str
choices:
- winrm
- ansible.builtin.winrm
- psrp
- ansible.builtin.psrp
- ssh
- ansible.builtin.ssh
default: "winrm"
version_added: 2.3.0
api_version:
description:
- Specify the used KubeVirt API version.
@@ -183,8 +198,8 @@ TYPE_LOADBALANCER = "LoadBalancer"
TYPE_NODEPORT = "NodePort"
ID_MSWINDOWS = "mswindows"
SERVICE_TARGET_PORT_SSH = 22
SERVICE_TARGET_PORT_WINRM_HTTP = 5985
SERVICE_TARGET_PORT_WINRM_HTTPS = 5986
SERVICE_TARGET_PORT_WIN_MGMT_HTTP = 5985
SERVICE_TARGET_PORT_WIN_MGMT_HTTPS = 5986
class KubeVirtInventoryException(Exception):
@@ -208,6 +223,7 @@ class InventoryOptions:
create_groups: Optional[bool] = None
base_domain: Optional[str] = None
append_base_domain: Optional[bool] = None
default_win_ansible_connection: Optional[str] = None
host_format: Optional[str] = None
namespaces: Optional[List[str]] = None
name: Optional[str] = None
@@ -263,6 +279,11 @@ class InventoryOptions:
if self.append_base_domain is not None
else config_data.get("append_base_domain", False)
)
self.default_win_ansible_connection = (
self.default_win_ansible_connection
if self.default_win_ansible_connection is not None
else config_data.get("default_win_ansible_connection", "winrm")
)
self.host_format = (
self.host_format
if self.host_format is not None
@@ -657,8 +678,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
or ports[0].get("targetPort")
not in [
SERVICE_TARGET_PORT_SSH,
SERVICE_TARGET_PORT_WINRM_HTTP,
SERVICE_TARGET_PORT_WINRM_HTTPS,
SERVICE_TARGET_PORT_WIN_MGMT_HTTP,
SERVICE_TARGET_PORT_WIN_MGMT_HTTPS,
]
):
continue
@@ -790,23 +811,36 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
)
# Set up the connection
service = None
if self._is_windows(
ansible_connection = (
self.inventory.get_host(hostname).get_vars().get("ansible_connection")
)
if ansible_connection is None and self._is_windows(
vmi["status"].get("guestOSInfo", {}),
vmi["metadata"].get("annotations", {}),
):
self.inventory.set_variable(hostname, "ansible_connection", "winrm")
service = self._find_service_with_target_port(
_services, SERVICE_TARGET_PORT_WINRM_HTTPS
ansible_connection = opts.default_win_ansible_connection
self.inventory.set_variable(
hostname, "ansible_connection", ansible_connection
)
if service is None:
service = self._find_service_with_target_port(
_services, SERVICE_TARGET_PORT_WINRM_HTTP
)
else:
service = None
if ansible_connection in (None, "ssh", "ansible.builtin.ssh"):
service = self._find_service_with_target_port(
_services, SERVICE_TARGET_PORT_SSH
)
elif ansible_connection in (
"winrm",
"ansible.builtin.winrm",
"psrp",
"ansible.builtin.psrp",
):
service = self._find_service_with_target_port(
_services, SERVICE_TARGET_PORT_WIN_MGMT_HTTPS
)
if service is None:
service = self._find_service_with_target_port(
_services, SERVICE_TARGET_PORT_WIN_MGMT_HTTP
)
self._set_ansible_host_and_port(
vmi,