大爱

Python fabric远程自动部署简介

2014-04-21

Fabric是一个Python(2.5-2.7)库,用于简化使用SSH的应用程序部署或系统管理任务。

它提供的操作包括:执行本地或远程shell命令,上传/下载文件,以及其他辅助功能,如提示用户输入、中止执行等。

本文主要介绍CentOS 6.3上使用Fabric进行自动化部署的基本方法。

1. 环境部署

本节主要介绍python版本升级,pip及fabric安装方法。

1.1 Python版本升级

CentOS 6.3自带的Python版本为2.6,首先需要升级到2.7版本。由于旧版本的Python已被深度依赖,所以不能卸载原有的Python,只能全新安装。

1) 下载Python,选择Gzipped source tarball,下载地址:https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz

2) 解压安装,命令如下

1
2
3
4
5
$ tar -xvf Python-2.7.14.tgz
$ cd Python-2.7.14
$ ./configure --prefix=/usr/local/python2.7
$ make
$ make install
  1. 创建链接来使系统默认python变为python2.7
1
$ ln -fs /usr/local/python2.7/bin/python2.7 /usr/bin/python
  1. 查看Python版本
1
$ python –V
  1. 修改yum配置(否则yum无法正常运行)
1
$ vi /usr/bin/yum

将第一行的#!/usr/bin/python修改为系统原有的python版本地址#!/usr/bin/python2.6

至此CentOS 6.3系统Python已成功升级至最新2.7.14版本。

1.2 安装pip

Pip是一个安装和管理python包的工具。

安装方法如下:

1) 下载pip,地址:https://raw.github.com/pypa/pip/master/contrib/get-pip.py

2) 执行安装命令

1
$ python get-pip.py
  1. 创建连接(否则会报错提示“命令不存在”)
1
$ ln -s /usr/local/python2.7/bin/pip /usr/bin/pip

1.3 安装fabric

1) 执行安装命令

1
$ pip install fabric

2) 创建连接(否则会报错提示“命令不存在”)

1
$ ln -s /usr/local/python2.7/bin/fab /usr/bin/fab

2. Fabric脚本编写

本节对fabric用法进行简单介绍,并提供实例以供参考。

2.1 Hello,fab

1) 在当前目录下新建文件fabfile.py,输入内容如下:

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

2) 执行命令fab hello,结果如下:

1
2
3
$ fab hello

Hello fab!

3) 文件名不为fabfile.py时需使用-f进行指定:

1
2
3
4
5
6
7
8
9
10
11
$ mv fabfile.py test.py

$ fab hello

Fatal error: Couldn't find any fabfiles!

Remember that -f can be used to specify fabfile path, and use -h for help.

$ fab -f test.py hello

Hello fab!

4) 参数传递

使用vi fabfile.py,修改fabfile.py:

1
2
def hello(name):
print 'Hello %s!'%name

可以通过如下两种方式进行参数传递:

1
2
3
4
5
6
7
$ fab hello:name=fab

Hello fab!

$ fab hello:fab

Hello fab!

2.2 本地操作

执行本地操作命令使用local

1) fabfile.py脚本内容如下

1
2
3
4
5
from fabric.api import local

def test():
local('cd /home/')
local('ls -l|wc -l')

2) 执行命令fab test,结果如下

1
2
3
4
5
6
7
$ fab test

[localhost] local: cd /home/

[localhost] local: ls -l|wc -l

8

2.3 远程操作

执行远程操作命令使用run

1) fabfile.py脚本内容如下

1
2
3
4
5
6
7
8
from fabric.api import cd,run,env,hosts

env.hosts=['192.168.85.99:22','192.168.85.101:22']
env.password='test'

def test():
with cd('/home'):
run("du -sh")

2) 执行命令fab test,结果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ fab test

[192.168.85.99:22] Executing task 'test'

[192.168.85.99:22] run: du -sh

[192.168.85.99:22] out: 392G .

[192.168.85.99:22] out:

[192.168.85.101:22] Executing task 'test'

[192.168.85.101:22] run: du -sh

[192.168.85.101:22] out: 5.6G .

[192.168.85.101:22] out:

Disconnecting from 192.168.85.99... done.

Disconnecting from 192.168.85.101... done.

3) 多服务器混合,需要在不同服务器进行不同操作时,可参考如下脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from fabric.api import env,roles,run,execute

env.roledefs = {'server1': ['root@192.168.85.99:22',],'server2': ['root@192.168.85.100:22', ]}
env.password = 'test'

@roles('server1')
def task1():
run('ls /home/ -l | wc -l')

@roles('server2')
def task2():
run('du -sh /home')

def test():
execute(task1)
execute(task2)

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ fab test

[root@192.168.85.99:22] Executing task 'task1'

[root@192.168.85.99:22] run: ls /home/ -l | wc -l

[root@192.168.85.99:22] out: 27

[root@192.168.85.99:22] out:

[root@192.168.85.100:22] Executing task 'task2'

[root@192.168.85.100:22] run: du -sh /home

[root@192.168.85.100:22] out: 1.4G /home

[root@192.168.85.100:22] out:

Disconnecting from 192.168.85.99... done.

Disconnecting from 192.168.85.100... done.

3. 参考

上面只是对Python+Fabric自动部署脚本编写方法的简单介绍,在实际应用过程中根据具体需求编写相应的脚本时可以参考如下文章:

1) http://docs.fabfile.org/en/latest/index.html

2) http://wklken.me/posts/2013/03/25/python-tool-fabric.html

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章