- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python的自动化部署模块Fabric的安装及使用指南由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
fabric是python2.5或者更高的库,可以通过ssh在多个host上批量执行任务.完成系统管理任务.它提供一套基本操作在本地和远程执行shell命令,或者上传下载文件,辅助提供用户输入或终止执行. 。
下面安装fabric模块有2种方法
1.使用easy_install(下面是debain5环境) 。
1
2
3
4
5
6
7
8
9
|
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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环境) 。
1
2
3
|
apt-get
install
python-pip
pip
install
fabric
apt-get
install
python-paramiko
|
导入模块未报错说明安装成功. 。
实例
1.在调用fabric的时候使用命令行参数,-H 指定哪台主机 。
1
|
root@10.1.6.201:python
# cat fabfile4.py
|
1
2
3
4
5
6
7
|
#!/usr/bin/env python
#coding=utf-8
from
fabric.api
import
*
def
printMem():
cmd_output
=
run(
'free -m'
)
print
cmd_output
|
1
2
3
4
5
6
7
8
9
|
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:
|
。
1
2
3
4
5
6
7
|
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.也就是环境变量. 。
1
|
root@10.1.6.201:python
# vim fabfile.py
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/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()
|
1
|
root@10.1.6.201:python
# python fabfile.py #脚本执行
|
1
2
3
4
5
|
[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:
|
1
|
root@10.1.6.201:python
# vim fabfile1.py
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/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'
)
|
1
|
root@10.1.6.201:python
# fab test1 -f fabfile.py #使用fab指定任务执行,注意文件后默认跟fabfile.py
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[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协议上传下载文件 。
1
|
root@10.1.6.201:python
# cat fabfile1.py
|
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/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)
|
。
1
|
root@10.1.6.201:python
# fab final -f fabfile1.py
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
[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 <- /home/ftp/a.txt
[localhost] local: ls -l /tmp
total 31688
-rw-r--r-- 1 root root 6 May 29 22:29 a.txt
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
Done.
Disconnecting from 10.1.1.45:22000... done.
|
上面实例中只列举了几个常用的farbic环境变量.如env.hosts,env.password等,可以不需要交互输入密码. 。
以下还有常用环境变量以供参考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
from
fabric.api
import
run, roles
env.roledefs
=
{
'db'
: [
'db1'
,
'db2'
],
'web'
: [
'web1'
,
'web2'
,
'web3'
],
}
@roles
(
'db'
)
def
migrate():
# Database stuff here.
pass
@roles
(
'web'
)
def
update():
# Code updates here.
pass
|
fab也可以使用命令设置环境变量,常用命令 。
最后此篇关于Python的自动化部署模块Fabric的安装及使用指南的文章就讲到这里了,如果你想了解更多关于Python的自动化部署模块Fabric的安装及使用指南的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我刚刚偶然发现Fabric并且文档并没有真正说明它是如何工作的。 我有根据的猜测是您需要在客户端和服务器端都安装它。 Python 代码存储在客户端,并在命令运行时通过 Fabric 的有线协议(pr
以下是我对 Hyperledger Fabric 系统的理解。如果有任何不正确的地方,请告诉我。 1) 所以我们有同行(背书人、 anchor 人、将军(拥有分类帐的人)、排序人)。此外,当我们设置结
我部署了一个持久性级别为 Silver 的全新 Service Fabric 集群,fabric:/System/InfrastructureService/FE 服务运行状况不佳,并出现以下错误:
我对使用 Fabric 很感兴趣,api 在未连接到互联网时如何工作? 我发现 firebase(与此工具类似的工具)会将数据存储在本地,然后对服务器进行批量更新。但是,超过 72 小时的任何内容都不
Fabric CA Server 和 Fabric CA Client 的功能是什么? Fabric CA 服务器和 Fabric CA 客户端的角色是什么? 谢谢 最佳答案 Fabric CA 提供
我想在不同的环境中使用相同的应用程序,我在 documentation 中找到了它, Apps with Multiple Environments Let’s say you have an app
我目前正在尝试学习 Hyperledger Fabric,我设法了解了如何设置网络(Orderer、Peers 等),但现在是链代码的一部分。 但是,我发现两个不同的 git 存储库(据我所知)可用于
我在 following this tutorial 时遇到错误 cannot convert from 'System.Fabric.StatelessServiceContext' to 'Sys
在我没有改变任何东西之前它可以工作,但今天我收到了这个错误,这里是我的 gradle buildscript { repositories { mavenCentral() maven
Fabric beta 手动分发有时不会发送邀请电子邮件,即使在它向该人显示“已邀请”的网页中也是如此。如果我点击重新发送邀请,那个人就会收到。我的 QA 团队人员必须一直来找我重新发送邀请。我知道
默认情况下,Hyperledger Fabric 在将许多证书保存到区 block 链之前将其存储在交易中。无论如何或任何想法/技术来最小化区 block 链中的交易大小? 任何想法都非常有用! 最佳
我是 Hyperledger Fabric 的新手。我正在阅读 Fabric 的文档最新版本,但我不清楚 Fabric 的共识。 Fabric 使用的共识是什么?它是如何工作的?请解释。 最佳答案 我
我是 Hyperledger Fabric 的新手,我正在尝试在本地向服务器注册 Fabric 客户端。这是我到目前为止所做的。 go get -u github.com/hyperledger/fa
Hyperledger Fabric 中链和状态数据库的主要区别是什么。我很困惑它们是否相同。 最佳答案 Hyperledger Fabric 中有两个“存储”数据的地方: 账本 状态数据库 账本是真
我尝试将图像上传到分类帐中(将图像转换为 base64 字符串并将其作为交易中的 arg 传递)。 当我发送大小为 30 kb 的图像时,它工作正常,但是对于 100 kb 的图像,我的交易失败了,指
我正在尝试在初始化 Fabric CA 服务器后注册管理员: fabric-ca-server init -b “admin:adminpw” 并启动 CA 服务器: fabric-ca-serve
我正在使用来自 super 账本fabric firstsample的cryptogen工具,它未在 crypto-config/peerorganisation/org1.example.com/m
我离开了一家公司,用于访问 Fabric 的电子邮件已被删除。 现在我在同一家公司工作,他们用相同的句柄重新创建了电子邮件。我不知道这是否是问题所在,但我没有收到任何包含报告的电子邮件,也没有收到 B
如果我有 1 个组织 orgA ,在这个组织下我有 2 个用户:user1和 user2 , 在 orgA 中也有 1 个对等点,让我们称之为 peer0 . 现在想象一下,user1的证书在orgA
全部, 据我所知,在 Hyperledger Fabric 环境中,orderer 将消息传递给 peer。如果有离线对等。恢复到 ON-LINE 时,消息如何传递给对端? orderer如何知道pe
我是一名优秀的程序员,十分优秀!