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)