Skip to content

Commit 3607f96

Browse files
committed
Support both libssh and paramiko
* Swap to `get_file` and `copy_file` from `Connection` in `network_cli.py` to support both `libssh` and `paramiko` * Restore the `network_cli` check
1 parent e23fbfb commit 3607f96

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

plugins/action/net_put.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import os
1111
import tempfile
1212

13+
from ansible.errors import AnsibleError
14+
from ansible.module_utils.common.text.converters import to_text
15+
from ansible.module_utils.connection import ConnectionError
1316
from ansible.plugins.action import ActionBase
1417
from ansible.utils.display import Display
1518
from ansible.utils.hashing import checksum
@@ -28,6 +31,17 @@ def __init__(self, *args, **kwargs):
2831
def run(self, tmp=None, task_vars=None):
2932
result = super(ActionModule, self).run(task_vars=task_vars)
3033

34+
persistent_connection = self._play_context.connection.split(".")[-1]
35+
if persistent_connection != "network_cli":
36+
# It is supported only with network_cli
37+
return {
38+
"failed": True,
39+
"msg": (
40+
"connection type %s is not valid for net_put module, please use fully "
41+
"qualified name of network_cli connection type" % self._play_context.connection
42+
),
43+
}
44+
3145
try:
3246
self._src = self._task.args.get("src")
3347
except KeyError as exc:
@@ -65,27 +79,20 @@ def run(self, tmp=None, task_vars=None):
6579
)
6680

6781
try:
68-
self._connection._ssh_type_conn.fetch_file(self._dest, fetched_fp, self._protocol)
82+
self._connection.get_file(self._dest, fetched_fp, self._protocol)
6983
except Exception as exc:
84+
error = to_text(exc).lower()
7085
if not (
71-
"Error receiving information about file" in exc.message
72-
and "No such file or directory" in exc.message
86+
"error receiving information about file" in error
87+
or "no such file or directory" in error
7388
):
7489
raise exc
75-
display.vv("The file is not present on the remote device")
76-
finally:
77-
self._connection._ssh_type_conn.reset()
78-
self._dest_checksum = checksum(fetched_fp)
90+
self._dest_checksum = checksum(fetched_fp)
7991

80-
try:
81-
if self._dest_checksum != self._rendered_checksum:
82-
self._connection._ssh_type_conn.put_file(
83-
self._loader.get_real_file(self._rendered_real_file),
84-
self._dest,
85-
self._protocol,
86-
)
87-
finally:
88-
self._connection._ssh_type_conn.reset()
92+
if self._dest_checksum != self._rendered_checksum:
93+
self._connection.copy_file(
94+
self._loader.get_real_file(self._rendered_real_file), self._dest, self._protocol
95+
)
8996

9097
return result
9198
finally:

plugins/connection/libssh.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ def fetch_file(self, in_path, out_path, proto="sftp"):
630630
scp.get(in_path, out_path)
631631
except LibsshSCPException as exc:
632632
raise AnsibleError("Error transferring file from %s: %s" % (out_path, to_text(exc)))
633+
finally:
634+
self.reset()
633635
else:
634636
raise AnsibleError("Don't know how to transfer file over protocol %s" % proto)
635637

0 commit comments

Comments
 (0)