Skip to content

Commit aae13bd

Browse files
tpmullanphilpep
authored andcommitted
Improved ssh config support in Paramiko backend
The include statement is now supported when using paramiko as the backend
1 parent 30acc8a commit aae13bd

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

testinfra/backend/paramiko.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(
5454
self.timeout = int(timeout)
5555
super().__init__(self.host.name, *args, **kwargs)
5656

57-
def _load_ssh_config(self, client, cfg, ssh_config):
57+
def _load_ssh_config(self, client, cfg, ssh_config, ssh_config_dir="~/.ssh"):
5858
for key, value in ssh_config.lookup(self.host.name).items():
5959
if key == "hostname":
6060
cfg[key] = value
@@ -74,6 +74,14 @@ def _load_ssh_config(self, client, cfg, ssh_config):
7474
cfg["gss_kex"] = value == "yes"
7575
elif key == "proxycommand":
7676
cfg["sock"] = paramiko.ProxyCommand(value)
77+
elif key == "include":
78+
new_config_path = os.path.join(
79+
os.path.expanduser(ssh_config_dir), value
80+
)
81+
with open(new_config_path) as f:
82+
new_ssh_config = paramiko.SSHConfig()
83+
new_ssh_config.parse(f)
84+
self._load_ssh_config(client, cfg, new_ssh_config, ssh_config_dir)
7785

7886
@cached_property
7987
def client(self):
@@ -87,21 +95,25 @@ def client(self):
8795
"password": self.host.password,
8896
}
8997
if self.ssh_config:
98+
ssh_config_dir = os.path.dirname(self.ssh_config)
99+
90100
with open(self.ssh_config) as f:
91101
ssh_config = paramiko.SSHConfig()
92102
ssh_config.parse(f)
93-
self._load_ssh_config(client, cfg, ssh_config)
103+
self._load_ssh_config(client, cfg, ssh_config, ssh_config_dir)
94104
else:
95105
# fallback reading ~/.ssh/config
96106
default_ssh_config = os.path.join(os.path.expanduser("~"), ".ssh", "config")
107+
ssh_config_dir = os.path.dirname(default_ssh_config)
108+
97109
try:
98110
with open(default_ssh_config) as f:
99111
ssh_config = paramiko.SSHConfig()
100112
ssh_config.parse(f)
101113
except IOError:
102114
pass
103115
else:
104-
self._load_ssh_config(client, cfg, ssh_config)
116+
self._load_ssh_config(client, cfg, ssh_config, ssh_config_dir)
105117

106118
if self.ssh_identity_file:
107119
cfg["key_filename"] = self.ssh_identity_file

0 commit comments

Comments
 (0)