Installing ansible-pylibssh in Ubuntu 24.04 on WSL 2

I got this message…

[WARNING]: ansible-pylibssh not installed, falling back to paramiko

and I couldn’t update my DNS-records on my switch, because Ansible failed with…

    "msg": "Failed to authenticate: Private key file is encrypted"

Which is ridiculous, because of course it is, but I’m also using an ssh-agent, so that shouldn’t be a problem. On to the great internet!

These people on Reddit said I could try installling ansible-pylibssh: https://www.reddit.com/r/ansible/comments/wp4l87/after_endless_hours_of_debugging_ansiblepylibssh/ so let’s see how that’s done..

From https://pypi.org/project/ansible-pylibssh/ we get..

$ pip install \
    --extra-index-url=https://ansible.github.io/pylibssh/simple/ \
    --pre \
    ansible-pylibssh
zsh: command not found: pip

Oh great, I don’t have pip..

https://packaging.python.org/en/latest/tutorials/installing-packages/ says to use python -m ensurepip --default-pip which did nothing or try get-pip.py with the caveat that, you should run it like..

python get-pip.py --prefix=/usr/local/ 
# which gave me "/usr/local/local" permission denied, or
python get-pip.py --prefix=/usr/
# which gave me "/usr/local/bin/python......." permission denied

So at least it found the right path on the second try, but I still couldn’t run pip.

I found some pip error message about breaking system packages, you’ve seen it.. “use apt packages” like python3-xyz, so okay. Let’s try that..

$ sudo apt-get install python3-pip

and lo and behold.. I now had pip. On to pylibssh again…

$ pip install \
    --extra-index-url=https://ansible.github.io/pylibssh/simple/ \
    --pre \
    ansible-pylibssh
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.
    
    See /usr/share/doc/python3.12/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

Oh hai, I’ve missed you.. So I shouldn’t have installed ansible-pylibssh with pip in the first place, because I should install it as a system package.. haha, okay..

$ sudo apt install python-ansible-pylibssh
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package python-ansible-pylibssh

Okay, so wait, there is no system-package for, wait, is this a typo in my python and python3asdhfasdf

$ sudo apt install python3-ansible-pylibssh
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package python3-ansible-pylibssh

Nope, no packages. Okay, so what else, virtual environments.. I don’t want to manage that..

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

So we’re installing pipx now. From https://pipx.pypa.io/stable/installation/

$ sudo apt update
$ sudo apt install pipx
$ pipx ensurepath
$ which pipx
/usr/bin/pipx

Okay wow, no errors and I installed an entire piece of software, what now..

$ pipx install ansible-pylibssh

No apps associated with package ansible-pylibssh or its dependencies. 
    If you are attempting to install a library, pipx should not be used.
    Consider using pip or a similar tool instead.

ARE YOU KIDDING ME.. of course it’s not a standalone application, it’s a library, that I was suggested to install in a virtual environment, which I didn’t want to manage and pipx promised to manage it for me.

Is this my fault!? Yes. It is. I wanted to use my Ansible on another version of Ubuntu than I’m used to and I should know about python, pip, pipx, venv and the extra pylibssh, which sometimes helps it use the ssh-agent properly, and now I’ve spent a lot of time learning those things, so that’s kind of like a free gift in and of itself.

Honestly I feel for this user:

https://stackoverflow.com/questions/77266551/how-to-install-ansible-pylibssh-using-apt-instead-of-pip

And I pity the people in the comments suggesting to “just install it with pip” or “compile the binaries from scratch” and you know what, I’m going back there after this, and telling the user how they can install it with pip.

On this forum: https://community.cisco.com/t5/network-access-control/conecting-ansible-in-ubuntu-to-cisco-ios/td-p/5141745 Thorbjørn is a major champ and helps out, and makes a final suggestion to their slow descend into madness which goes..

# If you are to start from scratch
rm -r .venv
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install ansible ansible-pylibssh

(They remove the .venv first, because they tried a bunch of things and didn’t get closer..)

And of course.. of course, I’ll add a .venv to my workspace, here’s a .gitignore for that, you’re welcome.

#.gitignore 
**/.venv

And of course… I’ll just install ANOTHER copy of Ansible.. I don’t care.. my bandwidth is unlimited!

And of course, I now have to invoke Ansible through python, and I’m using the playbooks-command, so it now looks like…

$ python3 -m ansible playbook -i inventory.yml playbook.yml

And it works.

It works.

So I updated my local documentation.

And I thought why not write a single blogpost in 2024, with some useful collection of knowledge, and here you have my descend into madness.

.. Sometimes it feels like cheating to just gather a bunch of snippets from forums and other blogs, but it took considerably more than 5 minutes, and I did _not_ go in the correct direction all the time. Now I can.. until the next time I update Ubuntu.. or Python.. or Ansible.. Anyways, my new README.md, enjoy the formatting.

# Mikrotik Management using Ansible

```shell
# with an ssh-agent that authorizes access
$ ansible-playbook -i inventory.yml playbook.yml [-vvv]
```

## [WARNING]: ansible-pylibssh not installed, falling back to paramiko

```shell
# Ubuntu 24.04
# create a local virtual environment
$ python3 -m venv .venv
# activate it
$ source .venv/bin/activate
# install ansible and ansible-pylibssh
$ python3 -m pip install ansible ansible-pylibssh
# run the "playbook" ansible command through python
$ python3 -m ansible playbook -i inventory.yml playbook.yml -vvv
```

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.