{"id":1477,"library":"fabric","title":"Fabric SSH Automation","description":"Fabric is a high-level Python library for streamlining SSH command execution and application deployment. It is currently at version 3.2.3 and releases updates on an as-needed basis, typically for bug fixes or minor enhancements, building on Invoke and Paramiko.","status":"active","version":"3.2.3","language":"python","source_language":"en","source_url":"https://github.com/fabric/fabric","tags":["ssh","automation","remote execution","deployment","devops"],"install":[{"cmd":"pip install fabric","lang":"bash","label":"Install Fabric"}],"dependencies":[{"reason":"Fabric builds upon Invoke for its task execution framework and CLI.","package":"invoke","optional":false},{"reason":"Fabric uses Paramiko as its underlying SSH client library.","package":"paramiko","optional":false}],"imports":[{"note":"Fabric 2.x+ removed the global 'env' state from Fabric 1.x. All operations now happen on a 'Connection' object.","wrong":"from fabric.api import env","symbol":"Connection","correct":"from fabric import Connection"},{"note":"While 'task' might exist in 1.x 'fabric.api', the modern Fabric 2.x+ API directly imports from 'fabric'.","wrong":"from fabric.api import task","symbol":"task","correct":"from fabric import task"}],"quickstart":{"code":"# fabfile.py\nfrom fabric import Connection, task\n\n@task\ndef hello(c):\n    \"\"\"\n    Runs a simple command on the remote host.\n    Example usage: fab -H user@localhost hello\n    \n    Note: For 'localhost' to work, an SSH server must be running \n    and your user configured for SSH access.\n    \"\"\"\n    print(f\"Connecting to {c.host} as {c.user or 'default'}...\")\n    # Run a command, hiding the command itself from stdout, only showing output\n    result = c.run(\"echo Hello from $(hostname)\", hide=True)\n    print(f\"Output: {result.stdout.strip()}\")","lang":"python","description":"Create a `fabfile.py` with tasks. Each task receives a `Connection` object `c` as its first argument. Run tasks from the command line using `fab -H user@host task_name`."},"warnings":[{"fix":"Rewrite Fabric 1.x scripts to use the new `Connection` object-based API. Consult the Fabric 2.x migration guide for details.","message":"Fabric 2.x and 3.x are NOT backward compatible with Fabric 1.x. The API was completely rewritten, moving from global state ('env') to an object-oriented design centered around the 'Connection' object.","severity":"breaking","affected_versions":"1.x to 2.x+"},{"fix":"Access connection properties via `c.host`, `c.user`, `c.config`, etc., and call methods like `c.run()`, `c.sudo()`.","message":"Global state ('env') from Fabric 1.x is gone. Tasks now receive a `Connection` object (`c`) as their first argument, which holds all connection-specific information and methods.","severity":"gotcha","affected_versions":"2.x, 3.x"},{"fix":"Replace calls like `run('command')` with `c.run('command')`. Similarly for `sudo`, `local`, `put`, `get`.","message":"The global functions `run()`, `sudo()`, `local()`, `put()`, `get()` from Fabric 1.x are now methods of the `Connection` object (e.g., `c.run()`, `c.sudo()`).","severity":"gotcha","affected_versions":"2.x, 3.x"},{"fix":"Ensure all context manager usage is prefixed with the `Connection` object, e.g., `with c.cd('/tmp'): c.run('ls')`.","message":"Context managers like `cd()`, `warn_only()`, `settings()` from Fabric 1.x are also now methods of the `Connection` object (e.g., `with c.cd('/path'):`).","severity":"gotcha","affected_versions":"2.x, 3.x"},{"fix":"Define task parameters explicitly (`@task(arg1='default')`), or configure connection parameters via `Connection` constructor, or `fab` CLI flags (`-H`, `-u`, `-i`, `-p`).","message":"Configuration is handled via the `Config` object or directly on `Connection` instances, not global dictionaries. Passing parameters to tasks requires explicit arguments or configuration.","severity":"gotcha","affected_versions":"2.x, 3.x"}],"env_vars":null,"search_vec":"'3.2.3':25 'applic':18 'as-need':31 'autom':3,48 'basi':34 'bug':37 'build':42 'command':15 'current':22 'deploy':19,51 'devop':52 'enhanc':41 'execut':16,50 'fabric':1,4 'fix':38 'high':8 'high-level':7 'invok':44 'level':9 'librari':11 'minor':40 'need':33 'paramiko':46 'python':10 'releas':27 'remot':49 'ssh':2,14,47 'streamlin':13 'typic':35 'updat':28 'version':24","created_at":"2026-04-09T03:49:37.575696+00:00","updated_at":"2026-04-16T14:54:18.825755+00:00","problems":[{"fix":"Rewrite your imports to use the new Fabric 2.x/3.x API, primarily importing `Connection`, `task`, and other components directly from the `fabric` package. For example, instead of `from fabric.api import run`, use `from fabric import Connection, task` and call methods on a `Connection` object (e.g., `c = Connection('host'); c.run('command')`).","cause":"This error occurs when trying to import `fabric.api`, which was part of Fabric 1.x, but you have Fabric 2.x or 3.x installed. The API was completely re-architected in Fabric 2.0 and `fabric.api` no longer exists.","error":"ModuleNotFoundError: No module named 'fabric.api'"},{"fix":"Ensure you create a `Connection` object and call `run()` or `sudo()` as methods of that object. For example, `from fabric import Connection; c = Connection('your_host'); c.run('command')`.","cause":"This error happens when you're using Fabric 2.x or 3.x but attempting to call `run()` or `sudo()` as global functions (like in Fabric 1.x) instead of calling them as methods on an instantiated `Connection` object.","error":"AttributeError: 'Connection' object has no attribute 'run'"},{"fix":"To prevent Fabric from aborting and inspect the failure, use `with Connection(host).settings(warn_only=True): result = c.sudo('your_command')`. Then check `result.failed` or `result.return_code` to handle the error programmatically. Also, ensure the remote command is syntactically correct and the user has `sudo` privileges for it. If the error is 'sudo: cd: command not found', ensure `shell=True` (the default) is used to allow shell features like `cd` to work correctly within `sudo` calls.","cause":"This typically means a command executed via `sudo()` on the remote host failed and returned a non-zero exit code, which Fabric interprets as an error and by default aborts the execution. This can be due to incorrect commands, insufficient privileges, or a missing shell context.","error":"Fatal error: sudo() received nonzero return code 1 while executing!"},{"fix":"Verify that the SSH daemon is running on the remote server, firewalls (local and network) allow connections on port 22 (or your custom SSH port), the hostname/IP and port are correct in your Fabric `Connection` arguments, and that the remote server's SSH configuration is not explicitly denying access for your user or connection type.","cause":"This is a common SSH error indicating that the remote host actively refused the connection attempt. This is often not a Fabric library error itself, but rather an issue with the SSH server on the remote machine (e.g., not running, firewall blocking the port, incorrect hostname/port, or incorrect SSH daemon configuration).","error":"Connection refused"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"3.2.3","cli_name":"fab","cli_version":"Fabric 3.2.3","type":"library","homepage":"https://fabfile.org","github":"https://github.com/fabric/fabric","docs":"https://docs.fabfile.org","changelog":"https://www.fabfile.org/changelog.html","pypi":"https://pypi.org/project/fabric/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops","http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}