python的自动化部署模块fabric的安装及使用指南

fabric是python2.5或者更高的库,可以通过ssh在多个host上批量执行任务.完成系统管理任务.它提供一套基本操作在本地和远程执行shell命令,或者上传下载文件,辅助提供用户输入或终止执行.

下面安装fabric模块有2种方法:

1.使用easy_install(下面是debain5环境)

root@10.1.6.200:pshell# apt-get install python-dev (安装python头文件)
root@10.1.6.200:pshell# apt-get install python-setuptools (安装easy_install)
root@10.1.6.200:pshell# wget http://peak.telecommunity.com/dist/ez_setup.py
root@10.1.6.200:pshell# python ez_setup.py
root@10.1.6.200:pshell# easy_install fabric

searching for fabric
reading http://pypi.python.org/simple/fabric/
best match: fabric 1.6.1
downloading http://pypi.python.org/packages/source/f/fabric/fabric-1.6.1.tar.gz#md5=c318ac3f7011ede0be1ca9a20f435735
processing fabric-1.6.1.tar.gz
running fabric-1.6.1/setup.py -q bdist_egg –dist-dir /tmp/easy_install-cvulrs/fabric-1.6.1/egg-dist-tmp-zfnowy
warning: no previously-included files matching ‘*’ found under directory ‘docs/_build’
warning: no previously-included files matching ‘*.pyc’ found under directory ‘tests’
warning: no previously-included files matching ‘*.pyo’ found under directory ‘tests’
zip_safe flag not set; analyzing archive contents…
fabric.version: module references __file__
adding fabric 1.6.1 to easy-install.pth file
installing fab script to /usr/bin
….
installed /usr/lib/python2.5/site-packages/pycrypto-2.6-py2.5-linux-x86_64.egg
finished processing dependencies for fabric

2.使用pip(下面使用的是debian7环境)

apt-get install python-pip
pip install fabric
apt-get install python-paramiko

导入模块未报错说明安装成功.

实例:

1.在调用fabric的时候使用命令行参数,-h 指定哪台主机

root@10.1.6.201:python# cat fabfile4.py

#!/usr/bin/env python
#coding=utf-8
from fabric.api import *
def printmem():
cmd_output = run(‘free -m’)
print cmd_output

root@10.1.6.201:python# fab -h root@10.1.1.45 printmem -f fabfile4.py
[root@10.1.1.45] executing task ‘printmem’
[root@10.1.1.45] run: free -m
[root@10.1.1.45] login password for ‘root’: #提示输入密码
[root@10.1.1.45] out: total used free shared buffers cached
[root@10.1.1.45] out: mem: 1005 968 37 0 36 831
[root@10.1.1.45] out: -/+ buffers/cache: 100 904
[root@10.1.1.45] out: swap: 1913 0 1913
[root@10.1.1.45] out:

total used free shared buffers cached
mem: 1005 968 37 0 36 831
-/+ buffers/cache: 100 904
swap: 1913 0 1913
done.
disconnecting from 10.1.1.45:22000… done.

2.以上我们需要输入密码才能完成操作,怎样自动执行呢?可以在fabfile4文件中配置host.也就是环境变量.

root@10.1.6.201:python# vim fabfile.py

#!/usr/bin/env python
#coding=utf-8
from fabric.api import *
from fabric.context_managers import *
env.host_string = ‘10.1.1.45’
env.port = ‘22000’ #默认端口22,默认登录用户root
env.password=’passwd’
def test1():
with cd(‘/home’):
run(‘ls -l’)
test1()

root@10.1.6.201:python# python fabfile.py #脚本执行

[10.1.1.45] run: ls -l
[10.1.1.45] out: total 8
[10.1.1.45] out: drwxr-xr-x 2 debian debian 4096 2012-08-27 11:54 debian
[10.1.1.45] out: drwxr-xr-x 2 root nogroup 4096 2013-05-22 18:07 ftp
[10.1.1.45] out:

root@10.1.6.201:python# vim fabfile1.py

#!/usr/bin/env python
#coding=utf-8
from fabric.api import *
from fabric.context_managers import *
env.hosts = [‘10.1.6.200′,’10.1.1.45’]
env.port = ‘22000’
env.password=’passwd’
def test1():
with cd(‘/home’): #更改目录
run(‘ls -l’)

root@10.1.6.201:python# fab test1 -f fabfile.py #使用fab指定任务执行,注意文件后默认跟fabfile.py

[10.1.6.200] executing task ‘test1’
[10.1.6.200] run: ls -l
[10.1.6.200] out: total 24
[10.1.6.200] out: drwxr-xr-x 2 davehe davehe 4096 2013-02-27 10:00 davehe
[10.1.6.200] out: -rw-r–r– 1 root root 1990 2013-02-27 09:55 davehe.tar.gz
[10.1.6.200] out: -rw-r–r– 1 root root 396 2013-05-17 18:27 rsync_log_130517
[10.1.6.200] out: -rw-r–r– 1 root root 7916 2013-05-20 21:04 rsync_log_130520
[10.1.6.200] out: drwxr-xr-x 2 taomee taomee 4096 2013-01-29 04:27 taomee
[10.1.6.200] out:
[10.1.1.45] executing task ‘test1’
[10.1.1.45] run: ls -l
[10.1.1.45] out: total 8
[10.1.1.45] out: drwxr-xr-x 2 debian debian 4096 2012-08-27 11:54 debian
[10.1.1.45] out: drwxr-xr-x 2 root nogroup 4096 2013-05-22 18:07 ftp
[10.1.1.45] out:
done.
disconnecting from 10.1.1.45:22000… done.
disconnecting from 10.1.6.200:22000… done.

3.使用get/put.利用sftp协议上传下载文件

root@10.1.6.201:python# cat fabfile1.py

#!/usr/bin/env python
#coding=utf-8
from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *
env.hosts = [‘10.1.1.45’]
env.port = ‘22000’
env.password=’passwd’
def test1():
print(red(“i’m 201”))
local(‘ls -l /tmp’)
def test2():
print (green(“i’m get file 45 to 186”))
get(‘/home/ftp/a.txt’,’/tmp/’) #下载
# put(‘/tmp/’,’/home/ftp/’) #上传
local(‘ls -l /tmp’) #local运行本地命令
def final():
execute(test1)
execute(test2)

root@10.1.6.201:python# fab final -f fabfile1.py

[10.1.1.45] executing task ‘final’
[10.1.1.45] executing task ‘test1’
i’m 201
[localhost] local: ls -l /tmp
total 31684
drwxr-xr-x 2 root root 4096 may 13 22:08 bin
drwxr-xr-x 3 root root 4096 may 13 22:08 conf
drwxr-xr-x 6 root root 4096 may 13 22:08 etc
-rwxr-xr-x 1 root root 6797 may 13 22:08 init
-rw-r–r– 1 root root 32400896 may 13 22:07 initrd.img-3.2.0-4-amd64
drwxr-xr-x 6 root root 4096 may 13 22:08 lib
drwxr-xr-x 2 root root 4096 may 13 22:08 lib64
drwxr-xr-x 2 root root 4096 may 13 22:08 run
drwxr-xr-x 2 root root 4096 may 13 22:08 sbin
drwxr-xr-x 6 root root 4096 may 13 22:08 scripts
[10.1.1.45] executing task ‘test2’
i’m get file 45 to 186
[10.1.1.45] download: /tmp/a.txt

Posted in 未分类

发表评论