码迷,mamicode.com
首页 > 其他好文 > 详细

fabric部署详解

时间:2016-02-23 15:54:19      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

安装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

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

1.vim fabfile.py

技术分享
技术分享
def hello():
    print("Hello fab!")

 

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


Done.
技术分享

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.
技术分享

3.参数传递

技术分享
def hello(name):
    print ‘Hello %s!‘%name
技术分享
技术分享
[root@rs1 tmp]# fab hello:name=hy
Hello hy!

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

Done.
技术分享

 二、本地操作  local

vim fabfile.py  

技术分享
from fabric.api import local

def test():
    local(‘uname -a‘)

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.

 三、远程操作  run

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

vim fabfile.py  

技术分享
from fabric.api import run

def test():
    run(‘uname -a‘)

fab test

技术分享
技术分享
[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.
技术分享

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

技术分享
from fabric.api import run

def test(command):
           run(command)

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

技术分享 View Code

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

技术分享 View Code

fab test:‘uname -a‘

技术分享 View Code

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

技术分享 View Code

fab test:‘uname -a‘

技术分享 View Code

多台服务器

同种操作

fab test:‘uname -a

技术分享 View Code
技术分享 View Code

多种操作

1
2
3
4
5
[root@rs1 tmp]# fab -l
Available commands:
 
    test
    test1
技术分享 View Code

fab test:‘uname -a‘

技术分享 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.

fabric部署详解

标签:

原文地址:http://www.cnblogs.com/paul03/p/5210064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!