From 76d805d72381d6b5f5095c6bbdc74f262ee03840 Mon Sep 17 00:00:00 2001 From: Carsten Grohmann Date: Tue, 30 Apr 2024 15:32:35 +0200 Subject: [PATCH] Add check for the existance of ssh control path directory --- testinfra/backend/ssh.py | 2 +- testinfra/utils/ansible_runner.py | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/testinfra/backend/ssh.py b/testinfra/backend/ssh.py index a43d4a7f..aae144d5 100644 --- a/testinfra/backend/ssh.py +++ b/testinfra/backend/ssh.py @@ -27,7 +27,7 @@ def __init__( ssh_config: Optional[str] = None, ssh_identity_file: Optional[str] = None, timeout: int = 10, - controlpath: str = "", + controlpath: Optional[str] = None, controlpersist: int = 60, ssh_extra_args: Optional[str] = None, *args: Any, diff --git a/testinfra/utils/ansible_runner.py b/testinfra/utils/ansible_runner.py index 084c03b6..dc315141 100644 --- a/testinfra/utils/ansible_runner.py +++ b/testinfra/utils/ansible_runner.py @@ -187,13 +187,18 @@ def get_config( control_path = config.get("ssh_connection", "control_path", fallback="", raw=True) if control_path: - directory = config.get( + control_path_dir = config.get( "persistent_connection", "control_path_dir", fallback="~/.ansible/cp" ) - control_path = control_path % ({"directory": directory}) # noqa: S001 - # restore original "%%" - control_path = control_path.replace("%", "%%") - kwargs["controlpath"] = control_path + control_path_dir = os.path.expanduser(control_path_dir) + control_path_dir = os.path.normpath(control_path_dir) + + if os.path.isdir(control_path_dir): + control_path = control_path % ( # noqa: S001 + {"directory": control_path_dir} + ) + control_path = control_path.replace("%", "%%") # restore original "%%" + kwargs["controlpath"] = control_path spec = "{}://".format(connection)