{"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":"en","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,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}