官网地址html
1.安装python
pip install fabric
依赖 Paramiko 、PyCrypto库windows
如下依赖肯能要手动安装 api
#安装 pycrypto 密码库
pip install pycrypto
#安装 pycrypto 依赖
pip install ecdsa
ps:【windows7 x64 ,python2.7 】Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
能够安装一个Micorsoft Visual C++ Compiler for Python 2.7的包服务器
2.fabric使用
新建py脚本:fabfile.py并发
def hello(): print("Hello world!")
执行fab命令cors
fab hello
若是当前模块下没有fabfile.py文件,那么须要指定-f参数 python2.7
fab -f test.py hello
使用参数工具
def hello(name, value): print("%s = %s!" % (name, value))
fab hello:name=age,value=20
执行本地操做ui
from fabric.api import local, lcd def lsfab(): with lcd('~/tmp/fab'): local('ls')
远程操做
def setting_ci(): local('echo "add and commit settings in local"') def update_setting_remote(): print "remote update" with cd('~/temp'): #cd用于进入某个目录 run('ls -l | wc -l') #远程操做用run
多服务器操做
#!/usr/bin/env python # encoding: utf-8 from fabric.api import * env.roledefs = { 'testserver': ['user1@host1:port1',], 'productserver': ['user2@host2:port2', ] } @roles('testserver') def task1(): run('ls -l | wc -l') @roles('productserver') def task2(): run('ls ~/temp/ | wc -l') def do(): execute(task1) execute(task2)
用不一样颜色打印
from fabric.colors import * def show(): print green('success') print red('fail') print yellow('yellow')
fabric 命令行工具
装饰器做用? @task @parallel 命令行经常使用: fab --help fab -l -- 显示可用的task(命令) fab -H -- 指定host,支持多host逗号分开 fab -R -- 指定role,支持多个 fab -P -- 并发数,默认是串行 fab -w -- warn_only,默认是碰到异常直接abort退出 fab -f -- 指定入口文件,fab默认入口文件是:fabfile/fabfile.py
更多参见官网