Mastering Fabric: Automate Server Management and Deployments with Python
This article introduces Fabric, a Python-based automation tool for server management and application deployment, explains its core features, showcases real-world use cases like Instagram, and provides step‑by‑step code examples covering basic commands, parameter passing, local and remote execution, role‑based tasks, parallel execution, and installation instructions.
What is Fabric
Fabric is a Python‑based automation script tool for system management and application deployment that conveniently supports multiple servers. A single script file can execute the same or different tasks on many servers.
It provides a concise command set to perform common operations such as running shell commands locally or on remote servers, uploading/downloading files, and prompting user input for interactive operations. Advanced features include server grouping, parallel task execution, and error handling.
Use Cases
Instagram, a large‑scale image social app owned by Facebook, operates thousands of servers and performs more than 30 code deployments per day. Before adopting Facebook’s distributed deployment system, Instagram used Fabric for massive server management and application deployment, demonstrating Fabric’s powerful capabilities.
Usage Examples
(1) Hello World
Create a script file fabfile.py:
def hello():
print("Hello world!")Run the task: $ fab hello You can rename the script and specify it explicitly:
$ mv fabfile.py hello.py
$ fab -f hello.py hello(2) Passing Parameters
def hello(name, val):
print("%s %s!" % (name, val))Execute with parameters: $ fab hello:name=hi,val=world (3) Executing Local Commands
from fabric.api import local, lcd
def ls():
with lcd('~'):
local('ls')(4) Mixed Local and Remote Execution
from fabric.api import local, cd, run, env
env.hosts = ['[email protected]:28']
env.password = 'password'
def do_local():
local('echo "in local"')
def do_remote():
print "in remote"
with cd('~'):
run('ls -l')
def opt():
do_local()
do_remote()Run opt to see both local and remote results.
(5) Different Tasks on Different Servers
#!/usr/bin/env python
# encoding: utf-8
from fabric.api import *
env.roledefs = {
'testserver': ['[email protected]:28'],
'realserver': ['[email protected]:22'],
}
env.passwords = {
'[email protected]:28': "password",
'[email protected]:22': "password",
}
@roles('testserver')
def task1():
run('ls /home')
@roles('realserver')
def task2():
run('ls /home')
def dotask():
execute(task1)
execute(task2)Running dotask executes task1 on the test server and task2 on the real server.
Important Feature: Parallel Task Execution
Parallel execution is especially powerful when managing many servers, saving considerable time. By default tasks run sequentially. To run tasks in parallel, use the -P flag or the @parallel decorator.
Example of running two tasks on three hosts sequentially:
$ fab -H web1,web2,web3 update reloadResult:
update on web1
update on web2
update on web3
reload on web1
reload on web2
reload on web3Enable parallel execution:
$ fab -H web1,web2,web3 -P update reloadResult:
update on web1, web2, and web3
reload on web1, web2, and web3Alternatively, annotate tasks:
@parallel
def runs_in_parallel():
# ...
@serial
def runs_serially():
# ...Execute both:
$ fab -H host1,host2,host3 runs_in_parallel runs_seriallyResult shows the parallel task runs on all hosts simultaneously, while the serial task runs on each host one after another.
Installation
Fabric requires a Python environment. Install it via the system package manager:
Ubuntu: $ sudo apt-get install fabric CentOS: $ yum install fabric Official website: http://www.fabfile.org
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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
