Skip to content

Commit d0b0aff

Browse files
authored
wsl connection: import paramiko directly (#10531)
Import paramiko directly.
1 parent 3bb7a77 commit d0b0aff

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bugfixes:
2+
- "wsl connection plugin - avoid deprecated ansible-core paramiko import helper, import paramiko directly instead
3+
(https://github.com/ansible-collections/community.general/issues/10515, https://github.com/ansible-collections/community.general/pull/10531)."

plugins/connection/wsl.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@
315315
import shlex
316316
import socket
317317
import tempfile
318+
import traceback
318319
import typing as t
319320

320321
from ansible.errors import (
@@ -323,18 +324,24 @@
323324
AnsibleError,
324325
)
325326
from ansible_collections.community.general.plugins.module_utils._filelock import FileLock, LockTimeout
327+
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
326328
from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text
327-
from ansible.module_utils.compat.paramiko import PARAMIKO_IMPORT_ERR, paramiko
328-
from ansible.module_utils.compat.version import LooseVersion
329329
from ansible.playbook.play_context import PlayContext
330330
from ansible.plugins.connection import ConnectionBase
331331
from ansible.utils.display import Display
332332
from ansible.utils.path import makedirs_safe
333333
from binascii import hexlify
334334
from subprocess import list2cmdline
335335

336+
try:
337+
import paramiko
338+
PARAMIKO_IMPORT_ERR = None
339+
except ImportError:
340+
paramiko = None
341+
PARAMIKO_IMPORT_ERR = traceback.format_exc()
336342

337-
if t.TYPE_CHECKING and paramiko:
343+
344+
if t.TYPE_CHECKING and PARAMIKO_IMPORT_ERR is None:
338345
from paramiko import MissingHostKeyPolicy
339346
from paramiko.client import SSHClient
340347
from paramiko.pkey import PKey
@@ -437,7 +444,7 @@ def _parse_proxy_command(self, port: int = 22) -> dict[str, t.Any]:
437444
def _connect(self) -> Connection:
438445
""" activates the connection object """
439446

440-
if paramiko is None:
447+
if PARAMIKO_IMPORT_ERR is not None:
441448
raise AnsibleError(f'paramiko is not installed: {to_native(PARAMIKO_IMPORT_ERR)}')
442449

443450
port = self.get_option('port')

tests/unit/plugins/connection/test_wsl.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
from ansible_collections.community.general.plugins.module_utils._filelock import FileLock, LockTimeout
1313
from ansible.errors import AnsibleError, AnsibleAuthenticationFailure, AnsibleConnectionFailure
1414
from ansible.module_utils.common.text.converters import to_bytes
15-
from ansible.module_utils.compat.paramiko import paramiko
1615
from ansible.playbook.play_context import PlayContext
1716
from ansible.plugins.loader import connection_loader
1817
from io import StringIO
1918
from pathlib import Path
2019
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch, MagicMock, mock_open
2120

2221

23-
pytest.importorskip('paramiko')
22+
paramiko = pytest.importorskip('paramiko')
2423

2524

2625
@pytest.fixture

0 commit comments

Comments
 (0)