Python fabric远程自动部署简介

本文介绍如何使用Python结合fabric实现自动化部署脚本的编写,包括基本的hello世界示例、执行本地与远程操作命令等,并提供了多服务器混合操作的示例脚本。

2.1.    Hello,fab

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

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

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

1# fab hello
2 
3Hello fab!

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

1# mv fabfile.py test.py
2 
3# fab hello
4 
5  
6 
7Fatal error: Couldn't find any fabfiles!
8 
9  
10 
11Remember that -f can be used to specify fabfile path, and use -h for help.
12 
13  
14 
15# fab -f test.py hello
16 
17Hello fab!

4. 参数传递

1#vi fabfile.py
2 
3def hello(name):
4 
5        print 'Hello %s!'%name
6 
7  
8 
9# fab hello:name=fab
10 
11Hello fab!
12 
13  
14 
15  
16 
17# fab hello:fab
18 
19Hello fab!

2.2.    本地操作

执行本地操作命令使用local

1. fabfile.py脚本内容如下

1from fabric.api import local
2 
3  
4 
5def test():
6 
7local('cd /home/')
8 
9local('ls -l|wc -l')

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

1# fab test
2 
3[localhost] local: cd /home/
4 
5[localhost] local: ls -l|wc -l
6 
78

2.3.    远程操作

执行远程操作命令使用run

1. fabfile.py脚本内容如下

1from fabric.api import cd,run,env,hosts
2 
3env.hosts=['192.168.85.99:22','192.168.85.101:22']
4 
5env.password='test'
6 
7def test():
8 
9with cd('/home'):
10 
11run("du -sh")

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

1# fab test
2 
3[192.168.85.99:22] Executing task 'test'
4 
5[192.168.85.99:22] run: du -sh
6 
7[192.168.85.99:22] out: 392G      .
8 
9[192.168.85.99:22] out:
10 
11[192.168.85.101:22] Executing task 'test'
12 
13[192.168.85.101:22] run: du -sh
14 
15[192.168.85.101:22] out: 5.6G     .
16 
17[192.168.85.101:22] out:
18 
19Disconnecting from 192.168.85.99... done.
20 
21Disconnecting from 192.168.85.101... done.

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

1from fabric.api import env,roles,run,execute
2 
3env.roledefs = {
4 
5'server1': ['root@192.168.85.99:22',],
6 
7'server2': ['root@192.168.85.100:22', ]
8 
9}
10 
11env.password = 'test'
12 
13@roles('server1')
14 
15def task1():
16 
17run('ls /home/ -l | wc -l')
18 
19@roles('server2')
20 
21def task2():
22 
23run('du -sh /home')
24 
25def test():
26 
27execute(task1)
28 
29execute(task2)

结果如下

1# fab test
2 
3[root@192.168.85.99:22] Executing task 'task1'
4 
5[root@192.168.85.99:22] run: ls /home/ -l | wc -l
6 
7[root@192.168.85.99:22] out: 27
8 
9[root@192.168.85.99:22] out:
10 
11[root@192.168.85.100:22] Executing task 'task2'
12 
13[root@192.168.85.100:22] run: du -sh /home
14 
15[root@192.168.85.100:22] out: 1.4G   /home
16 
17[root@192.168.85.100:22] out:
18 
19Disconnecting from 192.168.85.99... done.
20 
21Disconnecting 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

转载于:https://www.cnblogs.com/ievjai/p/4601262.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值