Introduction to Fabric: Python Library for Remote SSH Task Automation
Fabric is a Python library that lets you write task scripts and execute them over SSH on multiple hosts, providing a powerful way to automate deployments, system administration, and other large‑scale remote operations.
Fabric Overview
Fabric is a Python library that enables batch execution of tasks over SSH on multiple hosts. You write task scripts locally and Fabric runs them remotely, which is ideal for automated deployment and system administration.
Official site: http://www.fabfile.org/
Chinese site: http://fabric-chs.readthedocs.io/zh_CN/chs/
Common Environment Variables
Fabric stores environment variables in a dictionary fabric.state.env, which is also exposed via fabric.api. The shortcut env is usually used. Variables such as env.user and env.password can override the default local SSH user and suppress interactive password prompts.
Typical variables include env.hosts, env.roledefs, env.key_filename, etc.
Execution Model
Fabric executes tasks sequentially by default, but a parallel mode is available. The execution follows these priority rules:
A task list is created and passed to fab via the command line.
Each task has an associated host list defined by variables.
Tasks are run in order; for each task, Fabric iterates over its host list.
If a host list is empty, the task runs locally once.
Common APIs
Typical usage scenarios include:
Viewing local and remote host information simultaneously.
Dynamically retrieving remote directory listings.
Uploading and downloading files in gateway mode.
Fabric Installation and Usage
Installation
pip install fabric
easy_install fabric
# or source installationThe fab command is installed into Python's bin directory; you may need to create a symbolic link:
[root@saltstack ~]# find / -type f -name "fab"
/usr/local/python2.7.10/bin/fab
[root@saltstack ~]# ln -s /usr/local/python2.7.10/bin/fab /usr/bin/fabUse fab -h or fab --help for help (see screenshot below).
Fabric Application Cases
Case 1: Execute a series of local operations
Case 2: Remote server type inspection
Case 3: Passing parameters to remote hosts
Case 4: Batch execution on multiple servers
Case 5: Mixed operations on different servers
Extensions
Extension 1: Colored output
Resulting screenshot:
Extension 2: Error and exception handling – by default, a failed command stops the subsequent commands, but Fabric allows custom handling as described in the documentation.
Extension 3: Password management – Fabric supports SSH public‑key authentication and a two‑level password system: a default password via env.password for servers sharing the same credential, and a per‑host mapping via env.passwords.
Summary
Using Fabric you can manage a collection of hosts (including hostname, user, and password), define task functions, and flexibly decide which tasks run on which hosts. This is especially useful for large‑scale host management scenarios such as operations, private‑cloud management, and automated application deployment.
The article provides an introductory overview; Fabric also offers advanced features like role definitions, interactive remote sessions, exception handling, parallel execution, file operations, and can be invoked programmatically from Python applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
