From dd1331120a6a877ed4fd2da0a299ce704c31ad7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Sat, 28 Feb 2026 19:06:57 +0100 Subject: [PATCH] Allow VSOCK as Ansible host From https://man.archlinux.org/man/vsock.7: > The VSOCK address family facilitates communication between virtual machines and the host they are running on. An example: https://wiki.archlinux.org/title/QEMU#Accessing_SSH_via_vsock The separator between the "vsock" identifier and the CID can either be a forward slash (`/`) or a percent sign (`%`). `urllib.parse.urlparse` will mistake the forward slash for the beginning of the URL path, but a percent sign is accepted. ``` >>> urlparse("ansible://vsock/555") ParseResult(scheme='ansible', netloc='vsock', path='/555', params='', query='', fragment='') >>> urlparse("ansible://vsock%555") ParseResult(scheme='ansible', netloc='vsock%555', path='', params='', query='', fragment='') ``` --- test/test_backends.py | 7 +++++++ testinfra/utils/ansible_runner.py | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/test/test_backends.py b/test/test_backends.py index 39cdecba..131e35da 100644 --- a/test/test_backends.py +++ b/test/test_backends.py @@ -212,6 +212,13 @@ def get_vars(host): "host.name": "host", }, ), + ( + {}, + b"host ansible_host=vsock%555", + { + "host.name": "vsock%555", + }, + ), ( {}, b"host ansible_connection=smart", diff --git a/testinfra/utils/ansible_runner.py b/testinfra/utils/ansible_runner.py index eebaa553..69c3997b 100644 --- a/testinfra/utils/ansible_runner.py +++ b/testinfra/utils/ansible_runner.py @@ -17,6 +17,7 @@ import json import os import tempfile +import urllib.parse from collections.abc import Iterator from typing import Any, Callable, Optional, Union @@ -214,7 +215,7 @@ def get_config( if version == 6: spec += "[" + testinfra_host + "]" else: - spec += testinfra_host + spec += urllib.parse.quote(testinfra_host) if port: spec += f":{port}" return testinfra.get_host(spec, **kwargs)