question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get pyinfra work with ssh

See original GitHub issue

Describe the bug

Similarly to #671, trying to access a server via the pyinfra ssh operator I keep getting the error “Host key verification failed”.

I’m not sure it’s a bug or rather I’m doing something wrong.

To Reproduce

inventory.py

home_devices = [
    ('tk',{
        'ssh_port': 22,
        'ssh_user': 'paolo',
        'ssh_key': '~/.ssh/foo/id_ed25519_tk',
        'ssh_key_password': '',
    }),
]

deploy.py

from pyinfra.operations import ssh

ssh.command(
    name='System uptime',
    user='paolo',
    hostname='tk',
    command='uptime',
)

Run with:

$ pyinfra inventory.py deploy.py -vvv --debug

I also tried to run pyinfra tk deploy.py -vvv --debug without positive results.

Output:

$ pyinfra tk deploy.py -vvv --debug
    [pyinfra_cli.main] Checking potential directory: 
    [pyinfra_cli.main] Deploy directory remains as cwd
--> Loading config...
--> Loading inventory...
    [pyinfra_cli.inventory] Creating fake inventory...

--> Connecting to hosts...
    [pyinfra.api.connectors.ssh] Connecting to: tk ({'allow_agent': True, 'look_for_keys': True, 'hostname': 'tk', 'timeout': 10})
    [tk] Connected
    [pyinfra.api.state] Activating host: tk

--> Preparing operations...
    Loading: deploy.py
    [pyinfra.api.operation] Adding operation, {'System uptime'}, opOrder=(3,), opHash=_________________
    [tk] Ready: deploy.py

--> Proposed changes:
    Ungrouped:
    [tk]   Operations: 1   Commands: 1   

--> Beginning operation run...
--> Starting operation: System uptime 
    [pyinfra.api.operations] Starting operation System uptime on tk
    [pyinfra.api.connectors.ssh] Running command on tk: (pty=None) sh -c 'ssh -p 22 paolo@tk uptime'
[tk] >>> sh -c 'ssh -p 22 paolo@tk uptime'
[tk] Host key verification failed.
    [pyinfra.api.connectors.ssh] Waiting for exit status...
    [pyinfra.api.connectors.ssh] Command exit status: 255
    [tk] Error
    [pyinfra.api.state] Failing hosts: tk
--> pyinfra error: No hosts remaining!

Note 1: if I directly type sh -c 'ssh -p 22 paolo@tk uptime' everything works

Note 2: I have this entry in .ssh/config

Host tk
  User paolo
  HostName tk
  IdentityFile ~/.ssh/foo/id_ed25519_tk

Commenting those lines the output changes to

--> Connecting to hosts...
    [pyinfra.api.connectors.ssh] Connecting to: tk ({'allow_agent': True, 'look_for_keys': True, 'hostname': 'tk', 'timeout': 10})
    [tk] Authentication error ()
    [pyinfra.api.state] Failing hosts: tk
--> pyinfra error: No hosts remaining!

Note 3: removing the key with ssh-keygen -R tk, pyinfra correctly recognize that

...
--> Connecting to hosts...
    [pyinfra.api.connectors.ssh] Connecting to: tk ({'allow_agent': True, 'look_for_keys': True, 'hostname': 'tk', 'timeout': 10})
    No host key for tk found in known_hosts
    [tk] Connected
    [pyinfra.api.state] Activating host: tk
...

Expected behavior

SSH connection to the remote host should happen without errors.

Meta

pyinfra --support

--> Support information:

    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: Linux
      Platform: Linux-5.11.0-37-generic-x86_64-with-glibc2.29
      Release: 5.11.0-37-generic
      Machine: x86_64
    pyinfra: v1.4.17
    Executable: /home/paolo/tmpvenv/bin/pyinfra
    Python: 3.8.12 (CPython, GCC 9.3.0)

pyinfra was installed with pip

$ pip install -U pip
$ pip install pyinfra

$ pip freeze
bcrypt==3.2.0
certifi==2021.10.8
cffi==1.15.0
charset-normalizer==2.0.7
click==8.0.3
colorama==0.4.4
configparser==5.0.2
cryptography==35.0.0
distro==1.5.0
gevent==21.8.0
greenlet==1.1.2
idna==3.3
Jinja2==3.0.2
MarkupSafe==2.0.1
ntlm-auth==1.5.0
paramiko==2.8.0
pycparser==2.20
pyinfra==1.4.17
PyNaCl==1.4.0
python-dateutil==2.8.2
pywinrm==0.4.2
requests==2.26.0
requests-ntlm==1.1.0
six==1.16.0
urllib3==1.26.7
xmltodict==0.12.0
zope.event==4.5.0
zope.interface==5.4.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
paolodinacommented, Oct 21, 2021

@Fizzadar thankyou very much. It was my fault, works like a charm now! For me the issue is solved, close if you like.

0reactions
Fizzadarcommented, Jan 6, 2022

I’m going to close this issue now as it’s diverged from the original problem. There have been improvements to the auth error handling since that should display the underlying authentication issue if there is any. If needed please open another issue and we’ll get things sorted 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

SSH config ignored · Issue #299 · Fizzadar/pyinfra - GitHub
Describe the bug It seems that the ~/.ssh/config file is parsed, but not taken into account when connecting to hosts.
Read more >
Ssh Operations - pyinfra Documentation
Ssh Operations¶. Execute commands and up/download files from the remote host. Eg: pyinfra -> inventory-host.net <-> another-host.net.
Read more >
How to Deploy Python App on a Remote Server with Pyinfra
In this article, you will see how you can deploy a Python app (Flask/Django) with a configuration management tool pyinfra on a remote...
Read more >
Why you should try pyinfra | Lobsters
My question would be: Against this baseline, what problems does pyinfra solve? For example, Ansible can work on multiple servers in parallel. It ......
Read more >
pyinfra Changelog - PyUp.io
+ Use SSH connector parameters with `files.rsync` operation (StevenKGER) ... CLI flag has been added to set the working directory before `pyinfra` executes....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found