Skip to content

Commit 40eff52

Browse files
Add attribute to accept passphrase, when using a private key file. (#722)
* add attribute to accept passphrase * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add updates pre-commit * fix test --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent cff2eb0 commit 40eff52

File tree

7 files changed

+41
-8
lines changed

7 files changed

+41
-8
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repos:
66
- id: update-docs
77

88
- repo: https://github.com/pre-commit/pre-commit-hooks
9-
rev: v5.0.0
9+
rev: v6.0.0
1010
hooks:
1111
- id: check-merge-conflict
1212
- id: debug-statements
@@ -15,25 +15,25 @@ repos:
1515
- id: trailing-whitespace
1616

1717
- repo: https://github.com/pycontribs/mirrors-prettier
18-
rev: "v3.3.3"
18+
rev: "v3.6.2"
1919
hooks:
2020
- id: prettier
2121
additional_dependencies:
2222
- prettier
2323
- prettier-plugin-toml
2424

2525
- repo: https://github.com/PyCQA/isort
26-
rev: 5.13.2
26+
rev: 6.0.1
2727
hooks:
2828
- id: isort
2929
args: ["--filter-files"]
3030

3131
- repo: https://github.com/psf/black
32-
rev: 24.10.0
32+
rev: 25.1.0
3333
hooks:
3434
- id: black
3535

3636
- repo: https://github.com/pycqa/flake8
37-
rev: 7.1.1
37+
rev: 7.3.0
3838
hooks:
3939
- id: flake8
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bugfixes:
2+
- "Added support for private key passphrase in libssh connection plugin, when using encrypted private keys specified by the C(ansible_private_key_file) attribute."
3+
- "Set libssh logging level to DEBUG when Ansible verbosity is greater than 3, to aid in troubleshooting connection issues."

docs/ansible.netcommon.libssh_connection.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ Parameters
196196
<div>Requires ansible-pylibssh version &gt;= 1.0.0</div>
197197
</td>
198198
</tr>
199+
<tr>
200+
<td colspan="1">
201+
<div class="ansibleOptionAnchor" id="parameter-"></div>
202+
<b>private_key_passphrase</b>
203+
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
204+
<div style="font-size: small">
205+
<span style="color: purple">string</span>
206+
</div>
207+
</td>
208+
<td>
209+
</td>
210+
<td>
211+
<div>var: ansible_private_key_password</div>
212+
<div>var: ansible_private_key_passphrase</div>
213+
</td>
214+
<td>
215+
<div>Passphrase used to unlock the private key specified by the <code>ansible_private_key_file</code> attribute.</div>
216+
<div>This is required if the private key is encrypted with a passphrase.</div>
217+
</td>
218+
</tr>
199219
<tr>
200220
<td colspan="1">
201221
<div class="ansibleOptionAnchor" id="parameter-"></div>

plugins/connection/libssh.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@
6565
vars:
6666
- name: ansible_libssh_password_prompt
6767
version_added: 3.1.0
68+
private_key_passphrase:
69+
description:
70+
- Passphrase used to unlock the private key specified by the C(ansible_private_key_file) attribute.
71+
- This is required if the private key is encrypted with a passphrase.
72+
type: string
73+
vars:
74+
- name: ansible_private_key_password
75+
- name: ansible_private_key_passphrase
6876
host_key_auto_add:
6977
description: 'TODO: write it'
7078
env: [{name: ANSIBLE_LIBSSH_HOST_KEY_AUTO_ADD}]
@@ -389,7 +397,7 @@ def _connect_uncached(self):
389397
self.ssh = Session()
390398

391399
if display.verbosity > 3:
392-
self.ssh.set_log_level(logging.INFO)
400+
self.ssh.set_log_level(logging.DEBUG)
393401

394402
self.keyfile = os.path.expanduser("~/.ssh/known_hosts")
395403

@@ -432,6 +440,7 @@ def _connect_uncached(self):
432440
password=self.get_option("password"),
433441
password_prompt=self.get_option("password_prompt"),
434442
private_key=private_key,
443+
private_key_password=self.get_option("private_key_passphrase"),
435444
timeout=self._play_context.timeout,
436445
port=port,
437446
**ssh_connect_kwargs,

plugins/module_utils/cli_parser/cli_parsertemplate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
1010
# SPDX-License-Identifier: BSD-2-Clause
1111

12-
""" A shim class for the NetworkTemplate
12+
"""A shim class for the NetworkTemplate
1313
this was done in case there is a need to
1414
modify the resource module parser class
1515
or extend it a split it from the cli parsers.

plugins/sub_plugins/cli_parser/pyats_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,5 @@ def parse(self, *_args, **_kwargs):
137137
parsed = device.parse(command, output=cli_output)
138138
except Exception as exc:
139139
msg = "The pyats library return an error for '{cmd}' for '{os}'. Error: {err}."
140-
return {"errors": [(msg.format(cmd=command, os=network_os, err=to_native(exc)))]}
140+
return {"errors": [msg.format(cmd=command, os=network_os, err=to_native(exc))]}
141141
return {"parsed": parsed}

tests/unit/plugins/connection/test_libssh.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def test_libssh_connect(conn, monkeypatch):
5858
look_for_keys=True,
5959
password="test",
6060
password_prompt=None,
61+
private_key_password=None,
6162
port=8080,
6263
timeout=60,
6364
user="user1",

0 commit comments

Comments
 (0)