You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
connectors.ssh: encrypted ssh_config IdentityFile prompts for passphrase even when it's already unlocked in ssh-agent #2066
Since 3.11, if a host's ~/.ssh/config has an IdentityFile pointing at an encrypted private key, pyinfra now loads and decrypts that key file itself and if no passphrase is known, it blocks waiting for one via a terminal prompt.
The problem is it does this even when ssh-agent (or a Keychain-backed agent) already has the key unlocked and ready to use. Now pyinfra prompts for the passphrase regardless, which breaks any workflow built around ssh-add/agent-based key management (e.g. ssh-add --apple-load-keychain on macOS). You get an unexpected blocking prompt on every run even though the connection would succeed instantly via the agent.
To Reproduce
Have an encrypted private key referenced via IdentityFile in ~/.ssh/config for some host.
Load that key into ssh-agent (or macOS Keychain) so agent auth would succeed without a passphrase.
Run any pyinfra command against that host with no ssh_key/ssh_key_password set in the inventory: hosts = [("my-host.example.com", {})] pyinfra inventory.py exec -- echo hi
Expected behavior
before trying to load and decrypt the IdentityFile itself, check whether the agent already has a matching key loaded and prefer that.
Meta
> pyinfra --support
If you are having issues with pyinfra or wish to make feature requests, please
check out the GitHub issues at https://github.com/Fizzadar/pyinfra/issues .
When adding an issue, be sure to include the following:
System: Darwin
Platform: macOS-15.7.3-arm64-arm-64bit-Mach-O
Release: 24.6.0
Machine: arm64
pyinfra: v3.11.0
cyclopts: v4.25.3
distro: v1.9.0
gevent: v26.9.0
jinja2: v3.1.6
packaging: v26.3
paramiko: v4.0.0
pydantic: v2.14.1
python-dateutil: v2.9.0.post0
rich: v15.0.0
typeguard: v4.6.0
types-paramiko: v4.0.0.20260518
typing-extensions: v4.16.0
Executable: /Users/kevinschweikert/projects/imen/it-automation/.venv/bin/pyinfra
Python: 3.14.7 (CPython, Clang 22.1.3 )
I just want to add that it's even more unergonomic than i feared. The password prompt will be made for every host in the inventory and if a reboot operation is in the deployment, the password prompt needs to be inputted again
I have the same problem, and it's throwing an ugly exception before asking me for the new private key.
If I enter the password for my key it works well.
An IdentityFile in ssh_config makes pyinfra set cfg["key_filename"] and call load_key_with_certificate() → ssh_util._load_private_key_file(), which prompts when the key is encrypted.
The agent is never consulted.
Passing the right pubkey is actually enough:
paramiko.AgentKeyis a PKey whose sign_ssh_data goes to the agent. Hand it over as pkey and _attach_identity_with_certificate() skips the load and pops key_filename, so paramiko offers exactly that key, signed by the agent.
Final fallback: every agent key is tried... (not good, either)
This isn't only about the prompt. Two ways a connection does end up offering every loaded key:
no IdentityFile (and no ssh_key in the inventory)
an encrypted IdentityFile and not CLI mode — i.e. pyinfra.is_cli is False (pyinfra is used as a library): instead of prompting, the load raises, pyinfra swallows it and drops back to the agent. (There's no public way to get this behaviour in CLI mode; allow_prompt is internal.)
In both cases paramiko offers each loaded key in turn and the server counts every rejection. A handful of keys is enough to exhaust the usual six attempts and fail a connection that ssh completes on the first try, because it offers
only the public half of the key file.
ssh succeeds with just 1 when it knows the (encrypted) IdentityFile, and the key is in the agent.
(Evil) Workaround:
_orig=client.load_key_with_certificatedefload_key_with_certificate(*a, **kw):
kw["allow_prompt"] =False# never blocktry:
return_orig(*a, **kw) # plain keys/certs: unchangedexceptPyinfraError: # encrypted -> use the agent's copypub=Path(f"{Path(kw.get('key_filename') ora[0]).expanduser()}.pub")
ifnotpub.is_file():
raiseblob=pub.read_text().split()[1]
forkeyinparamiko.Agent().get_keys():
ifkey.get_base64() ==blob:
returnkey# AgentKey is a PKey; signs via agentraiseclient.load_key_with_certificate=load_key_with_certificate
Activity
kevinschweikert commentedon Nov 9, 2026
I just want to add that it's even more unergonomic than i feared. The password prompt will be made for every host in the inventory and if a reboot operation is in the deployment, the password prompt needs to be inputted again
cl-parsons commentedon Nov 17, 2026
I have the same problem, and it's throwing an ugly exception before asking me for the new private key.
If I enter the password for my key it works well.
OS: ArtixLinux
pid1: OpenRC
Terminal: foot
WM: sway (wayland)
inventory
.ssh/config
ssh-add
Output: (I press return on password prompt because the key is already loaded
smilingthax commentedon Nov 18, 2026
An
IdentityFilein ssh_config makes pyinfra setcfg["key_filename"]and callload_key_with_certificate()→ssh_util._load_private_key_file(), which prompts when the key is encrypted.The agent is never consulted.
Passing the right pubkey is actually enough:
paramiko.AgentKeyis aPKeywhosesign_ssh_datagoes to the agent. Hand it over aspkeyand_attach_identity_with_certificate()skips the load and popskey_filename, so paramiko offers exactly that key, signed by the agent.Final fallback: every agent key is tried... (not good, either)
This isn't only about the prompt. Two ways a connection does end up offering every loaded key:
IdentityFile(and nossh_keyin the inventory)IdentityFileand not CLI mode — i.e.pyinfra.is_cliisFalse(pyinfra is used as a library): instead of prompting, the load raises, pyinfra swallows it and drops back to the agent. (There's no public way to get this behaviour in CLI mode;allow_promptis internal.)In both cases paramiko offers each loaded key in turn and the server counts every rejection. A handful of keys is enough to exhaust the usual six attempts and fail a connection that
sshcompletes on the first try, because it offersonly the public half of the key file.
sshsucceeds with just 1 when it knows the (encrypted) IdentityFile, and the key is in the agent.(Evil) Workaround: