fabric部署

安装fabric

pip install fabric

ln -s /usr/local/python2.7/bin/fab /usr/bin/fab  #创建软连接(创建fab为环境变量,如果python安装目录没有自定义,就无需此次操作)

测试

本地:rs1

远程 :

rs2,ip:192.168.11.190;

rs3,ip:192.168.11.20;

rs4,ip:192.168.11.6

 

一、基本用法(fab -l 查看可用的命令)

1.vim fabfile.py

def hello():
    print("Hello fab!")

 

 [root@rs1 tmp]# fab hello
Hello fab!


Done.
View Code

2.文件名不为fabfile.py时需进行指定

mv fabfile.py test.py
[root@rs1 tmp]# fab hello
Fatal error: Couldn't find any fabfiles!
Remember that -f can be used to specify fabfile path, and use -h for help.
Aborting.
[root@rs1 tmp]# fab -f test.py hello
Hello fab!

Done.
[root@rs1 tmp]# fab hello -f test.py
Hello fab!

Done.
View Code

3.参数传递

def hello(name):
    print 'Hello %s!'%name
View Code
[root@rs1 tmp]# fab hello:name=hy
Hello hy!

Done.
[root@rs1 tmp]# fab hello:hy
Hello hy!

Done.
View Code

 二、本地操作  local

vim fabfile.py  

from fabric.api import local

def test():
    local('uname -a')
View Code

fab test

[localhost] local: uname -a
Linux rs1 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux

Done.
View Code

 三、远程操作  run

 1.要执行的命令在脚本里

vim fabfile.py  

from fabric.api import run

def test():
    run('uname -a')
View Code

fab test -H root@192.168.11.190

[root@192.168.11.190] Executing task 'test'
[root@192.168.11.190] run: uname -a
[root@192.168.11.190] Login password for 'root': 
[root@192.168.11.190] out: Linux rs2 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux
[root@192.168.11.190] out: 


Done.
Disconnecting from 192.168.11.190... done.
View Code

2.自己传要执行哪些命令

from fabric.api import run

def test(command):
           run(command)
View Code

fab test:'uname -a' -H root@192.168.11.190

[root@192.168.11.190] Executing task 'remote_run'
[root@192.168.11.190] run: uname -a
[root@192.168.11.190] Login password for 'root': 
[root@192.168.11.190] out: Linux rs2 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux
[root@192.168.11.190] out: 


Done.
Disconnecting from 192.168.11.190... done.
View Code

 3.执行时省略-H。host放在脚本里

from fabric.api import (run, env, roles, hosts)

@hosts('root@192.168.11.190')
def test(command):
    run(command)
View Code

fab test:'uname -a'

[root@192.168.11.190] Executing task 'test'
[root@192.168.11.190] run: uname -a
[root@192.168.11.190] Login password for 'root': 
[root@192.168.11.190] out: Linux rs2 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux
[root@192.168.11.190] out: 


Done.
Disconnecting from 192.168.11.190... done.
View Code

 4.host、密码放在脚本里,执行时不用指定

from fabric.api import (run, env, roles, hosts)

env.hosts=['root@192.168.11.190']
env.password='toor'

def test(command):
    run(command)
View Code

fab test:'uname -a'

[root@192.168.11.190] Executing task 'test'
[root@192.168.11.190] run: uname -a
[root@192.168.11.190] out: Linux rs2 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux
[root@192.168.11.190] out: 


Done.
Disconnecting from 192.168.11.190... done.
View Code

5.多台服务器

同种操作

fab test:'uname -a

from fabric.api import *

env.hosts=['root@192.168.11.190','root@192.168.11.20']

env.passwords={'root@192.168.11.190:22':'toor','root@192.168.11.20:22':"121"}

def test(command):
    run(command)
View Code
[root@rs1 tmp]#  fab test:'uname -a'
[root@192.168.11.190] Executing task 'test'
[root@192.168.11.190] run: uname -a
[root@192.168.11.190] out: Linux rs2 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux
[root@192.168.11.190] out: 

[root@192.168.11.20] Executing task 'test'
[root@192.168.11.20] run: uname -a
[root@192.168.11.20] out: Linux rs3 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux
[root@192.168.11.20] out: 


Done.
Disconnecting from 192.168.11.20... done.
Disconnecting from 192.168.11.190... done.
View Code

多种操作

[root@rs1 tmp]# fab -l
Available commands:

    test
    test1
#coding:utf-8
from fabric.api import *

env.roledefs = {
'servergroup1':['root@192.168.11.190:22','root@192.168.11.6:22'],
'servergroup2':['root@192.168.11.20:22']
}

env.passwords={
'root@192.168.11.190:22':'toor',
'root@192.168.11.6:22':'1314',
'root@192.168.11.20:22':'121',
}

@roles('servergroup1')
def test(command):
    run(command)
    print 'servergroup1执行完毕,请指示'

@roles('servergroup2')
def test1(command):
    run(command)
    print 'servergroup2执行完毕,请指示'
View Code

fab test:'uname -a'

[root@192.168.11.190:22] Executing task 'test'
[root@192.168.11.190:22] run: uname -a
[root@192.168.11.190:22] out: Linux rs2 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux
[root@192.168.11.190:22] out: 

servergroup1执行完毕,请指示
[root@192.168.11.6:22] Executing task 'test'
[root@192.168.11.6:22] run: uname -a
[root@192.168.11.6:22] out: Linux rs3 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@192.168.11.6:22] out: 

servergroup1执行完毕,请指示

Done.
Disconnecting from 192.168.11.190... done.
Disconnecting from 192.168.11.6... done.
View Code

fab test1:'uname -a'

[root@192.168.11.20:22] Executing task 'test1'
[root@192.168.11.20:22] run: uname -a
[root@192.168.11.20:22] out: Linux rs3 2.6.32-504.el6.i686 #1 SMP Wed Oct 15 03:02:07 UTC 2014 i686 i686 i386 GNU/Linux
[root@192.168.11.20:22] out: 

servergroup2执行完毕,请指示

Done.
Disconnecting from 192.168.11.20... done.
View Code

6.有一个需求,把本地文件压缩并打包,再上传到多台服务器,解压缩

touch rs1.txt

from fabric.api import *

env.roledefs = { 
'servergroup1':['root@192.168.11.190:22','root@192.168.11.6:22'],
'servergroup2':['root@192.168.11.20:22']
}

env.passwords={
'root@192.168.11.190:22':'toor',
'root@192.168.11.6:22':'1314',
'root@192.168.11.20:22':'121',
}

@roles('servergroup1')
def tar_and_put(project_file):
    """对本地项目打包压缩、上传,在远程解压"""
    tar_file = project_file + '.tar.gz'
    local('tar -zcvf %s %s' % (tar_file,project_file))
    put(tar_file,'/tmp')
    with cd('/tmp'):
        run('tar zxvf %s' % tar_file)
        print 'servergroup1执行完毕,请指示'
View Code
[root@rs1 tmp]# fab -l
Available commands:

    tar_and_put  对本地项目打包压缩、上传,在远程解压

 fab tar_and_put:'rs1.txt'

[root@192.168.11.190:22] Executing task 'tar_and_put'
[localhost] local: tar -zcvf rs1.txt.tar.gz rs1.txt
rs1.txt
[root@192.168.11.190:22] put: rs1.txt.tar.gz -> /tmp/rs1.txt.tar.gz
[root@192.168.11.190:22] run: tar zxvf rs1.txt.tar.gz
[root@192.168.11.190:22] out: rs1.txt
[root@192.168.11.190:22] out: 

servergroup1执行完毕,请指示
[root@192.168.11.6:22] Executing task 'tar_and_put'
[localhost] local: tar -zcvf rs1.txt.tar.gz rs1.txt
rs1.txt
[root@192.168.11.6:22] put: rs1.txt.tar.gz -> /tmp/rs1.txt.tar.gz
[root@192.168.11.6:22] run: tar zxvf rs1.txt.tar.gz
[root@192.168.11.6:22] out: rs1.txt
[root@192.168.11.6:22] out: 

servergroup1执行完毕,请指示

Done.
Disconnecting from 192.168.11.190... done.
Disconnecting from 192.168.11.6... done.
View Code
[root@rs2 tmp]# ls
1.py  rs1.txt  rs1.txt.tar.gz
[root@rs4 tmp]# ls
mongodb-27017.sock  passwd  rs1.txt  rs1.txt.tar.gz  yum.log

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2016-02-22 16:05  沐风先生  阅读(287)  评论(0编辑  收藏  举报