官方网站:
http://docs.fabfile.org/en/1.6/
安装: # easy_install fabric 用法: # fab -h Options: -h, --help show this help message and exit -d NAME, --display=NAME print detailed info about command NAME -F FORMAT, --list-format=FORMAT formats --list, choices: short, normal, nested -I, --initial-password-prompt Force password prompt up-front -l, --list print list of possible commands and exit --set=KEY=VALUE,... comma separated KEY=VALUE pairs to set Fab env vars --shortlist alias for -F short --list -V, --version show program's version number and exit -a, --no_agent don't use the running SSH agent -A, --forward-agent forward local agent to remote end --abort-on-prompts abort instead of prompting (for password, host, etc) -c PATH, --config=PATH specify location of config file to use -D, --disable-known-hosts do not load user known_hosts file -e, --eagerly-disconnect disconnect from hosts as soon as possible -f PATH, --fabfile=PATH python module file to import, e.g. '../other.py' -g HOST, --gateway=HOST gateway host to connect through --hide=LEVELS comma-separated list of output levels to hide -H HOSTS, --hosts=HOSTS comma-separated list of hosts to operate on -i PATH path to SSH private key file. May be repeated. -k, --no-keys don't load private key files from ~/.ssh/ --keepalive=N enables a keepalive every N seconds --linewise print line-by-line instead of byte-by-byte -n M, --connection-attempts=M make M attempts to connect before giving up --no-pty do not use pseudo-terminal in run/sudo -p PASSWORD, --password=PASSWORD password for use with authentication and/or sudo -P, --parallel default to parallel execution method --port=PORT SSH connection port -r, --reject-unknown-hosts reject unknown hosts --system-known-hosts=SYSTEM_KNOWN_HOSTS load system known_hosts file before reading user known_hosts -R ROLES, --roles=ROLES comma-separated list of roles to operate on -s SHELL, --shell=SHELL specify a new shell, defaults to '/bin/bash -l -c' --show=LEVELS comma-separated list of output levels to show --skip-bad-hosts skip over hosts that can't be reached --ssh-config-path=PATH Path to SSH config file -t N, --timeout=N set connection timeout to N seconds -T N, --command-timeout=N set remote command timeout to N seconds -u USER, --user=USER username to use when connecting to remote hosts -w, --warn-only warn, instead of abort, when commands fail -x HOSTS, --exclude-hosts=HOSTS comma-separated list of hosts to exclude -z INT, --pool-size=INT number of concurrent processes to use in parallel mode
如果不是用-f选项,脚本文件名必须是fabfile.py,执行实例: # fab run_hide run_parallel run_parallel 如脚本所示,每个函数是一个任务。fab命令后跟任务名称,写几个就执行几次
fabfile.py脚本文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#!/usr/bin/env python
#coding:utf-
8
from fabric.contrib.files
import
*
from fabric.api
import
*
from fabric.colors
import
*
from fabric.tasks
import
*
###颜色输出
print(red(
" my "
) + green(
" name "
) + blue(
" is "
) + yellow(
" lishengjia "
))
###如果使用fab --ssh-config-path=选项的话,可以是配置文件中ssh相关选项生效(默认是不生效的)
env.usesshconfig = True
###设定远程登录的单用户
env.user =
'root'
###自定义用于认证的私钥
env.key_filename =
'/root/weibo'
###设置任务执行所用的全局默认主机列表
env.hosts = [
'10.13.82.232'
,
'10.13.82.233'
]
###角色的定义,为每个角色定义一个主机组,可以为主机组执行任务,优先级大于env.hosts
env.roledefs={
'tornado'
:[
'10.13.82.232'
,
'10.13.82.233'
],
'mops'
:[
'10.10.81.90'
]
}
@roles(
'tornado'
)
def run_test():
''
'this is two test'
''
run(
'ifconfig'
)
####并发
@parallel (或者@parallel(pool_size=
100
) 限定并发为
100
)
def run_parallel():
run(
'date>>test.txt'
)
###任务执行加参数(在命令行下的使用方式为:# fab nginx_log:btime=
1
,etime=
2
)
def nginx_log(btime,etime):
print btime,etime
####顺序执行,不加参数,默认就是顺序执行
@serial
def run_serial():
run(
'date>>test.txt'
)
run(
'sleep 1'
)
###进行与操作
def run_and():
with
prefix(
'cd /opt/'
):
with
prefix(
'ls'
):
run(
'ifconfig'
)
####远程主机目录切换
def run_dir():
with
cd(
'/tmp/'
):
run(
'ls'
)
with
cd(
'/opt/'
):
run(
'ls'
)
###上传文件到远端服务器
def run_put():
put(
'/root/1.sh'
,
'/root/1.sh'
)
###指定本次任务执行对应的ip,优先级较高,会忽略env.hosts和--list命令中host的设定
@hosts(
'10.13.82.232'
)
###从远端服务器下载文件
def run_get():
get
(
'/root/1.sh'
,
'/tmp/'
)
###执行出错命令的捕获,当执行
get
命令fail的时候,执行print语句
with
settings(warn_only=True):
if
get
(sfilename,dfilename).failed:
print
"warning:%s is not log!"
% partname
###本地切换工作目录及执行命令
def run_local():
lcd(
'/opt/scripts/'
)
local(
'ls'
)
local(
'ifconfig'
)
###给远程主机文件进行的操作
def run_append():
""
"this is a function to append file on remote host"
""
###远程主机文件追加
append(
'1.sh'
,
'my name is lee!'
)
###判断文件或者目录是否存在,存在就返回True
if
exists(
'/root/2.sh'
):
print
'ok'
else
:
print
"false"
###判断远程主机的文件中是否存在文本
if
contains(
'/root/1.sh'
,
'lee'
):
print
"OK"
###隐藏输出
def run_hide():
with
hide(
'running'
,
'stdout'
,
'stderr'
):
run(
'ls /var/www'
)
###多个任务一起执行
def run_tasks():
execute(run_local)
execute(run_append)
|
本文转自 leejia1989 51CTO博客,原文链接:http://blog.51cto.com/leejia/1202118,如需转载请自行联系原作者