# Fabric（部署工具）

**Fabric** 是一个开源 [Python](https://www.edgepedia.org/zh/python-programming-language) 库和命令行工具，用于通过 SSH 在远程计算机上执行 shell 命令，适用于应用部署和系统管理任务。<sup>[1](https://pypi.org/project/fabric/)</sup> 根据其文档，Fabric 是一个高层 Python 库，其目的是通过 SSH 在远程机器上运行 shell 命令并返回有用的 Python 对象；它依赖另外两个库：Invoke，负责管理子进程执行和命令行功能；Paramiko，提供 SSH 协议实现。<sup>[1](https://pypi.org/project/fabric/)</sup> Fabric 专注于应用层面的任务，如部署，而 Chef 和 [Puppet](https://www.edgepedia.org/zh/puppet-software) 等配置管理系统则倾向于管理服务器和系统库。<sup>[2](https://docs.python-guide.org/scenarios/admin/)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一个 Python 库和 `fab` 命令行工具，用于通过 SSH 在远程主机上运行 shell 命令和传输文件<sup>[1](https://pypi.org/project/fabric/)</sup> |
| 基础 | 构建于 Invoke（命令执行和 CLI 任务组织）和 Paramiko（SSH 与 SFTP 会话、密钥管理）之上<sup>[1](https://pypi.org/project/fabric/)</sup><sup> • </sup><sup>[3](https://docs.fabfile.org/en/stable/getting-started.html)</sup> |
| 核心 API | `Connection` 对象，提供 `run`、`sudo`、`put` 和 `get` 方法，返回 `Result` 对象<sup>[3](https://docs.fabfile.org/en/stable/getting-started.html)</sup> |
| 多主机执行 | `SerialGroup` 和 `ThreadingGroup` 封装多个连接，返回 `GroupResult` 对象<sup>[3](https://docs.fabfile.org/en/stable/getting-started.html)</sup> |
| 许可证与热度 | BSD-2-Clause 许可证；GitHub 上约 15,516 颗星和 1,955 个复刻<sup>[4](https://hysenlabs.com/projects/fabric-fabric)</sup> |
| 当前版本 | 3.2.3，发布于 2026 年 4 月 6 日<sup>[1](https://pypi.org/project/fabric/)</sup> |
| Python 支持 | Fabric 3.0.0（2023 年 1 月）放弃了对 Python 3.6 以下版本的支持，包括 Python 2<sup>[5](https://www.fabfile.org/changelog.html)</sup> |

## 工作原理

Fabric 最基本的用法是通过 SSH 在远程系统上执行一条 shell 命令，然后查询结果。`Connection` 对象代表一个 SSH 连接，提供 API 的核心功能；它至少需要一个主机名，并可通过关键字参数或 `[user@]host[:port]` 字符串指定用户名和端口号。<sup>[3](https://docs.fabfile.org/en/stable/getting-started.html)</sup> 调用 `run` 会将远程程序的输出打印到终端，并捕获到一个 `Result` 对象中，该对象暴露 `stdout`、`exited`（退出状态）、`ok`、`command` 以及发起该命令的连接等属性。<sup>[3](https://docs.fabfile.org/en/stable/getting-started.html)</sup>

**文件传输与权限提升。** 除了命令执行，`Connection.put` 和 `Connection.get` 通过 SFTP 处理文件传输，参数行为遵循 `cp` 和 `scp`。<sup>[3](https://docs.fabfile.org/en/stable/getting-started.html)</sup> 对于超级用户命令，Fabric 提供了一个基于 Invoke 自动应答机制的 `sudo` 辅助方法：用户提供 `sudo.password` 配置值，连接会自动处理密码提示。<sup>[3](https://docs.fabfile.org/en/stable/getting-started.html)</sup>

**多服务器。** 大多数实际用例涉及不止一台服务器。`Group` 封装一个或多个 `Connection` 对象，提供类似的 API；其具体子类包括 `SerialGroup`（在各主机上依次执行）和 `ThreadingGroup`（并发运行）。组方法返回 `GroupResult`，一个类似字典的对象，包含每个连接的结果；当个别连接失败时抛出 `GroupException`。<sup>[3](https://docs.fabfile.org/en/stable/getting-started.html)</sup>

**网关。** 对于内部主机无法直接访问的网络，Fabric 支持两种与 OpenSSH 客户端相对应的 SSH 网关方式：`ProxyJump` 风格使用 SSH 协议的 `direct-tcpip` 通道类型，更易使用、开销更小，且可嵌套；`ProxyCommand` 风格将内部连接通过任意本地子进程管道传输，允许 SOCKS 代理等技巧。文档建议在不确定时使用 `ProxyJump` 风格，同时为同一主机提供两种类型会引发异常。<sup>[6](https://docs.fabfile.org/en/stable/concepts/networking.html)</sup>

## fab 命令行工具

Fabric 代码可以通过 `fab` 从 shell 运行，它在 Invoke 的 CLI 机制之上封装了主机选择等功能。任务是在 `fabfile.py` 模块中用 `@task` 装饰的 Python 函数；`fab -H web1,web2,web3 upload_and_unpack` 会为每台主机运行一次该任务，每次调用传入该主机对应的 `Connection`。<sup>[3](https://docs.fabfile.org/en/stable/getting-started.html)</sup> 任务支持前置和后置任务、GNU 风格的参数化（如 `fab deploy --env=prod`），以及单次会话中的多任务（如 `fab build deploy`）。<sup>[7](https://www.fabfile.org/index.html)</sup>

这种 CLI 模式曾是 Fabric 1.x 的主要 API；从 Fabric 2.0 起，它只是一个便利层，超出其快捷方式范围的用例直接回退到库 API。<sup>[3](https://docs.fabfile.org/en/stable/getting-started.html)</sup> Fabric 2 的打包方式使其能与 Fabric 1 并存安装，让用户按自己的节奏升级。<sup>[7](https://www.fabfile.org/index.html)</sup>

## 版本历史

PyPI 的发布历史显示，Fabric 最早的版本发布于 2012 年 11 月 15 日，2.x 系列始于 2018 年，其中 2.5.0 于 2019 年 8 月发布，2.6.0 于 2021 年 1 月发布，2.7.1 于 2022 年 7 月发布。<sup>[1](https://pypi.org/project/fabric/)</sup> Fabric 3.0.0 于 2023 年 1 月 20 日发布，放弃了对 Python 3.6 以下版本的支持，并将 `inline_ssh_env` 的默认值改为 `True`，这对未显式设置该值就向 `run` 传递环境变量的代码来说是一个向后不兼容的变更。<sup>[5](https://www.fabfile.org/changelog.html)</sup> 后续 3.x 版本在 3.1.0 中增加了对 Paramiko 3.2 的 `AuthStrategy` 认证机制的可选支持，并在 3.2.0 中增强了 `fabric.testing` 模拟辅助工具；当前版本 3.2.3 于 2026 年 4 月 6 日发布。<sup>[5](https://www.fabfile.org/changelog.html)</sup><sup> • </sup><sup>[1](https://pypi.org/project/fabric/)</sup>

该项目由杰夫·福西耶（Jeff Forcier）维护，他在个人网站上维护路线图，文档站点与代码同步版本化。<sup>[7](https://www.fabfile.org/index.html)</sup><sup> • </sup><sup>[4](https://hysenlabs.com/projects/fabric-fabric)</sup>

## Fabric 与 Ansible 的比较

Fabric 和 [Ansible](https://www.edgepedia.org/zh/ansible-software) 在大多数情况下都使用 SSH 连接目标机器，但它们在模型和范围上有所不同。Fabric 通过 SSH 在目标机器上执行简单的命令行语句，使用 Python 编写，API 更直白、更基础；Ansible 使用 YAML，将模块推送到远程机器并在那里执行，为建模多层基础设施（如 Web 和数据库主机阵列）提供了更深入的语义。<sup>[8](https://www.sei.cmu.edu/blog/devops-technologies-fabric-or-ansible/)</sup> Fabric 可以在几分钟内得到结果，而 Ansible 需要更多精力去理解，但在配置管理方面通常更强大。<sup>[8](https://www.sei.cmu.edu/blog/devops-technologies-fabric-or-ansible/)</sup>

从实际用途来看：Fabric 适合从 Python 进行可重复的远程执行，例如从脚本在六台服务器上运行一条命令；而 Ansible 的清单、角色、剧本、幂等性和模块库是为将一组服务器收敛到已知状态而构建的。<sup>[4](https://hysenlabs.com/projects/fabric-fabric)</sup> 在比较评估中，Fabric 在易用性和灵活性方面评分较高，学习曲线低，但在可扩展性方面中等，适合轻量级脚本和较小项目，而非企业级自动化。<sup>[9](https://primeprompting.com/article/comparing-deployment-tools-for-python-ansible-saltstack-and-fabric-in-2026/)</sup>

## 其他名为 Fabric 的事物

Fabric 这个名字被几个不相关的项目使用，搜索结果经常将它们混淆。丹尼尔·米斯勒（Daniel Miessler）的 Fabric 是一个开源框架，通过众包的 AI 提示集用 AI 增强人类能力。<sup>[10](https://github.com/danielmiessler/fabric)</sup> [Microsoft Fabric](https://www.edgepedia.org/zh/microsoft-fabric) 是一个数据分析平台，其部署工具包括 `fabric-cicd` Python 库和 Fabric 部署管道——一种用于在开发、测试和生产阶段之间移动内容的应用程序生命周期管理工具。<sup>[11](https://github.com/MicrosoftDocs/fabric-docs/blob/main/docs/cicd/deployment-pipelines/intro-to-deployment-pipelines.md)</sup> 这些都与本文描述的 SSH 部署库无关。

## References

1. [fabric · PyPI](https://pypi.org/project/fabric/)
2. [Systems Administration, The Hitchhiker's Guide to Python](https://docs.python-guide.org/scenarios/admin/)
3. [Getting started, Fabric documentation](https://docs.fabfile.org/en/stable/getting-started.html)
4. [Fabric: Python remote execution over SSH, Hysen Labs](https://hysenlabs.com/projects/fabric-fabric)
5. [Changelog, Fabric documentation](https://www.fabfile.org/changelog.html)
6. [Networking, Fabric documentation](https://docs.fabfile.org/en/stable/concepts/networking.html)
7. [Welcome to Fabric!, fabfile.org](https://www.fabfile.org/index.html)
8. [DevOps Technologies: Fabric or Ansible, CMU Software Engineering Institute](https://www.sei.cmu.edu/blog/devops-technologies-fabric-or-ansible/)
9. [Comparing Deployment Tools for Python: Ansible, SaltStack, and Fabric in 2026, Prime Prompting](https://primeprompting.com/article/comparing-deployment-tools-for-python-ansible-saltstack-and-fabric-in-2026/)
10. [danielmiessler/Fabric, GitHub](https://github.com/danielmiessler/fabric)
11. [Introduction to deployment pipelines, Microsoft Fabric documentation](https://github.com/MicrosoftDocs/fabric-docs/blob/main/docs/cicd/deployment-pipelines/intro-to-deployment-pipelines.md)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Named software products and platforms › Cloud, virtualization, and containers › Workload schedulers and developer platforms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/fabric-deployment-tool

English original: https://www.edgepedia.org/fabric-deployment-tool
