Python fabric远程自动部署简介
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python fabric远程自动部署简介,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2620字,纯文字阅读大概需要4分钟。
内容图文
2.1. Hello,fab
1. 在当前目录下新建文件fabfile.py,输入内容如下
1
|
def hello():
|
2
|
3
|
print("Hello fab!")
|
2. 执行命令fab hello,结果如下
1
|
# fab hello
|
2
|
3
|
Hello fab!
|
3. 文件名不为fabfile.py时需进行指定
1
|
# mv fabfile.py test.py
|
2
|
3
|
# fab hello
|
4
|
5
|
|
6
|
7
|
Fatal error: Couldn‘t find any fabfiles!
|
8
|
9
|
|
10
|
11
|
Remember that -f can be used to specify fabfile path, and use -h for help.
|
12
|
13
|
|
14
|
15
|
# fab -f test.py hello
|
16
|
17
|
Hello fab!
|
4. 参数传递
1
|
#vi fabfile.py
|
2
|
3
|
def hello(name):
|
4
|
5
|
print ‘Hello %s!‘%name
|
6
|
7
|
|
8
|
9
|
# fab hello:name=fab
|
10
|
11
|
Hello fab!
|
12
|
13
|
|
14
|
15
|
|
16
|
17
|
# fab hello:fab
|
18
|
19
|
Hello fab!
|
2.2. 本地操作
执行本地操作命令使用local
1. fabfile.py脚本内容如下
1
|
from fabric.api import local
|
2
|
3
|
|
4
|
5
|
def test():
|
6
|
7
|
local(‘cd /home/‘)
|
8
|
9
|
local(‘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
|
7
|
8
|
2.3. 远程操作
执行远程操作命令使用run
1. fabfile.py脚本内容如下
1
|
from fabric.api import cd,run,env,hosts
|
2
|
3
|
env.hosts=[‘192.168.85.99:22‘,‘192.168.85.101:22‘]
|
4
|
5
|
env.password=‘test‘
|
6
|
7
|
def test():
|
8
|
9
|
with cd(‘/home‘):
|
10
|
11
|
run("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
|
19
|
Disconnecting from 192.168.85.99... done.
|
20
|
21
|
Disconnecting from 192.168.85.101... done.
|
3. 多服务器混合,需要在不同服务器进行不同操作时,可参考如下脚本
1
|
from fabric.api import env,roles,run,execute
|
2
|
3
|
env.roledefs = {
|
4
|
5
|
‘server1‘: [‘root@192.168.85.99:22‘,],
|
6
|
7
|
‘server2‘: [‘root@192.168.85.100:22‘, ]
|
8
|
9
|
}
|
10
|
11
|
env.password = ‘test‘
|
12
|
13
|
@roles(‘server1‘)
|
14
|
15
|
def task1():
|
16
|
17
|
run(‘ls /home/ -l | wc -l‘)
|
18
|
19
|
@roles(‘server2‘)
|
20
|
21
|
def task2():
|
22
|
23
|
run(‘du -sh /home‘)
|
24
|
25
|
def test():
|
26
|
27
|
execute(task1)
|
28
|
29
|
execute(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
|
19
|
Disconnecting from 192.168.85.99... done.
|
20
|
21
|
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
原文:http://www.cnblogs.com/ievjai/p/4601262.html
内容总结
以上是互联网集市为您收集整理的Python fabric远程自动部署简介全部内容,希望文章能够帮你解决Python fabric远程自动部署简介所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。