在我的github https://github.com/wangganyu188 上fork了別人的fabric 安裝包,
我的測試虛擬機是centos 6.2 64位的,安裝過程如下:
#yum install git
#git clone https://github.com/wangganyu188/fabric.git
#yum install python-setuptools python-devel gcc gcc-c++
# cd fabric
# python setup install
# fab #執行此命令,如果能執行,就ok了
使用方法,簡單測試如下
需要讀取的兩個文件,做爲參數傳遞,
ip.txt
192.168.10.134
192.168.10.135
192.168.10.140
192.168.10.141
192.168.10.152
ippwd.txt
192.168.10.134 123456
192.168.10.135 123456
192.168.10.140 123456
192.168.10.141 123456
192.168.10.142 123456
conf/config.py
#!/usr/bin/env python
#coding:utf8
#from fabric.api import local,cd,run,env,*
from fabric.api import *
import re,os
nciphost = []
ipdir = os.getcwd() + '/' + 'conf' + '/' + 'ip.txt'
ipwddir = os.getcwd() + '/' + 'conf' + '/' + 'ippwd.txt'
with open(ipdir,'r') as ipf:
for ip in ipf.readlines():
print ip,
nciphost.append(ip.strip('\n'))
print nciphost,
env.roledefs = {
'dn':[],
'nn':[],
'sn':[],
}
env.passwords = {}
with open(ipwddir,'r') as f:
for line in f.readlines():
for cv in nciphost:
b = re.match(r'^%s .*' %cv,line,re.M|re.I)
if b:
c = b.group()
k = c.split()[0]
v = c.split()[1]
nc='root@%s:22' %k
env.roledefs['dn'].append(nc)
env.roledefs['nn'].append(nc)
env.roledefs['sn'].append(nc)
env.passwords[nc]=v
conf/var.py
#!/usr/env python
Testcmd = "uptime"
main_hadoop.py
#!/usr/bin/python
#coding:utf-8
from fabric.api import *
from conf.config import *
from conf.var import *
@roles('dn')
def testcmd():
run(Testcmd)
def do_test():
execute(testcmd)