{"id":6512,"library":"ansible-pylibssh","title":"Ansible Pylibssh","description":"Ansible Pylibssh provides Python bindings for the `libssh` client library, specifically designed for Ansible's SSH connection needs. It offers a low-level interface to SSH functionalities. The library is actively maintained with regular stable releases, with the current version being 1.4.0, which was released in March 2026.","status":"active","version":"1.4.0","language":"en","source_language":"en","source_url":"https://github.com/ansible/pylibssh","tags":["ansible","ssh","libssh","networking","bindings","client"],"install":[{"cmd":"pip install ansible-pylibssh","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Requires the system-level libssh C library (version 0.9.0 or newer) to be installed for the Python bindings to function. This is a crucial runtime dependency, not a Python package.","package":"libssh","optional":false}],"imports":[{"note":"Core classes are exposed via the `pylibsshext` module, not directly from `ansible_pylibssh`.","wrong":"from ansible_pylibssh.session import Session","symbol":"Session","correct":"from pylibsshext.session import Session"},{"note":"Exceptions are located in the `pylibsshext.errors` submodule.","wrong":"from ansible_pylibssh.errors import LibsshSessionException","symbol":"LibsshSessionException","correct":"from pylibsshext.errors import LibsshSessionException"},{"note":"Library version information and other global attributes are available directly from `pylibsshext`.","symbol":"__version__","correct":"from pylibsshext import __version__"}],"quickstart":{"code":"import os\nfrom pylibsshext.errors import LibsshSessionException\nfrom pylibsshext.session import Session\n\nHOST = os.environ.get('SSH_HOST', 'localhost')\nUSER = os.environ.get('SSH_USER', 'testuser')\nPASSWORD = os.environ.get('SSH_PASSWORD', '')\n# Use a placeholder if not set, but real connections need a password or key\n\nif not PASSWORD:\n    print(\"Warning: SSH_PASSWORD environment variable not set. Connection might fail if password authentication is required.\")\n\nTIMEOUT = 30\nPORT = 22\n\nssh = Session()\ntry:\n    ssh.connect(\n        host=HOST,\n        user=USER,\n        password=PASSWORD,\n        timeout=TIMEOUT,\n        port=PORT,\n    )\n    print(f'Successfully connected to {HOST}:{PORT}.')\n    # Example: Execute a command\n    channel = ssh.open_session()\n    channel.request_exec('echo Hello from pylibssh!')\n    output = channel.read_stdout(2048).decode()\n    print(f'Command output: {output.strip()}')\n    channel.close()\n    channel.send_eof()\n\nexcept LibsshSessionException as ssh_exc:\n    print(f'Failed to connect to {HOST}:{PORT} over SSH: {ssh_exc!s}')\nexcept Exception as e:\n    print(f'An unexpected error occurred: {e}')\nfinally:\n    if ssh.is_connected:\n        ssh.disconnect()\n        print(f'Disconnected from {HOST}.')\n","lang":"python","description":"This quickstart demonstrates how to establish an SSH session, execute a simple command, and then properly disconnect. It uses environment variables for host, user, and password for security and flexibility. Ensure the `libssh` C library is installed on your system and replace `SSH_HOST` and `SSH_USER` with valid credentials if not using a passwordless setup."},"warnings":[{"fix":"Install `libssh` development packages (e.g., `libssh-dev` on Debian/Ubuntu, `libssh-devel` on RHEL/Fedora, or via Homebrew on macOS) matching version 0.9.0 or later on your operating system.","message":"The library is a set of Python bindings for `libssh`. Therefore, `libssh` (version 0.9.0 or newer) must be installed at the system level for `ansible-pylibssh` to function. Installing the Python package via pip alone is insufficient.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade your Python environment to Python 3.9 or newer before installing `ansible-pylibssh` version 1.4.0 or later.","message":"Support for Python 3.6, 3.7, and 3.8 was dropped with version 1.4.0. Installations on these Python versions will fail or result in an incompatible state.","severity":"breaking","affected_versions":"1.4.0 and later"},{"fix":"Upgrade `ansible-pylibssh` to version 1.2.2 or newer to ensure reliable SCP transfers for larger files.","message":"Older versions (prior to v1.2.2) had a bug where downloading files larger than 64kB over SCP could fail, leading to incomplete transfers.","severity":"gotcha","affected_versions":"< 1.2.2"},{"fix":"Upgrade `ansible-pylibssh` to version 1.2.1 or newer to prevent crashes when handling non-existent remote files during SCP operations.","message":"Prior to version 1.2.1, attempting to download a non-existent remote file via SCP could lead to program crashes.","severity":"gotcha","affected_versions":"< 1.2.1"},{"fix":"Upgrade `ansible-pylibssh` to version 1.2.0 or newer for improved stability and crash prevention related to channel handling.","message":"Earlier versions (prior to v1.2.0) could crash if `channel.recv` was called and `libssh` returned an `SSH_EOF` error, or if a channel was not explicitly closed.","severity":"gotcha","affected_versions":"< 1.2.0"},{"fix":"Ensure your macOS environment is version 15.0 or newer when installing `ansible-pylibssh` 1.4.0+ to use pre-compiled wheels. Alternatively, attempt to build from source (which may require specific build tools and `libssh` installation).","message":"As of version 1.4.0, macOS wheels require macOS version 15.0 or newer due to updates in the `cibuildwheel` process. Installations on older macOS versions may encounter issues.","severity":"breaking","affected_versions":"1.4.0 and later on macOS"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[{"fix":"Install 'ansible-pylibssh' using pip: 'pip install ansible-pylibssh'.","cause":"Ansible defaults to using the 'paramiko' SSH library because 'ansible-pylibssh' is not installed.","error":"[WARNING]: ansible-pylibssh not installed, falling back to paramiko"},{"fix":"Manually add the host key to the known_hosts file or configure Ansible to accept unknown host keys.","cause":"The SSH client cannot verify the host's identity because the host key is not in the known_hosts file.","error":"fatal: [10.96.192.10]: FAILED! => {\"changed\": false, \"msg\": \"paramiko: The authenticity of host '10.96.192.10' can't be established.\\nThe ssh-ed25519 key fingerprint is b'REDACTED'.\"}"},{"fix":"Ensure 'libssh' is installed on your system. On macOS, you can install it using Homebrew: 'brew install libssh'.","cause":"The linker cannot find the 'libssh' library during the installation of 'ansible-pylibssh'.","error":"ld: library not found for -lssh"},{"fix":"Install 'paramiko' using pip: 'pip install paramiko'.","cause":"Ansible falls back to 'paramiko' for SSH connections, but 'paramiko' is not installed.","error":"fatal: [10.2.201.194]: FAILED! => {\"changed\": false, \"msg\": \"paramiko is not installed: No module named 'paramiko'\"}"},{"fix":"Install `libssh` using your system's package manager (e.g., `brew install libssh` on macOS, `sudo apt-get install libssh-dev` on Debian/Ubuntu, `sudo dnf install libssh-devel` on RHEL/Fedora) and then install `ansible-pylibssh` using `pip` with appropriate CFLAGS if necessary, especially on macOS. For macOS with Homebrew, use: `CFLAGS=\"-I $(brew --prefix)/include -I ext -L $(brew --prefix)/lib -lssh\" pip install ansible-pylibssh`","cause":"ansible-pylibssh requires the `libssh` client library and its development headers (version 0.9.0 or newer) to be present and discoverable by the compiler during installation, which can be challenging on macOS or when building from source without proper CFLAGS.","error":"fatal error: 'libssh/libssh.h' file not found"}]}