fabric2.0 使用说明

4,349 阅读4分钟

fabric2.0 使用说明

fabric框架,主要的目的就是用来远程自动化部署。在最近,作者将fabric框架重写了一遍,升级到了2.0版本。在我学习过程中,遇到了不少的坑,最坑的一次就是python编译器老是给我提示,fabric 导入 api失败,没有fabric.api。我不断的怀疑自己是不是脑子瓦特了的时候,我pip list 查看了一下版本,再看了一下github版本。

我谢谢作者呕心沥血的更新框架。

本说明,结合官方文档,使用效果更佳。

安装

pip install fabric 

查看一下pip安装fabric的版本号:

fabric 2.1.3

一切OK,开始练习。

初步的使用

准备两台机器。一个pc,一个虚机就行。我的是两个虚机,ip地址如下:

虚机1: 192.168.11.11 系统:ubuntu

虚机2: 192.168.11.111 系统:centos

确保两个虚机都能使用SSH连接。

我的操作都是在(虚机1)上进行。。。

from fabric import Connection
In [3]: c = Connection('192.168.11.111', port=22, user='root', connect_kwargs={'password':'1'})

In [4]: result = c.run('uname -s')
Linux

代码说明:

我们一切的远程shell命令的执行都是基于Connection来实现的。实现的原理,也就是SSH。

Connection中一些参数:

  1. host 主机地址
  2. port 端口号
  3. user 用户名

连接的一些其他参数都放到connect_kwargs中。我这使用了密码连接。(试了半天,查看api手册才试对)

当我们获取到了Connection对象之后,我们就可以使用它来进行一些命令。

result是执行的结果,包含了许多属性值,如下:

In [8]: result.
result.command      result.connection   result.encoding     result.env          result.exited       result.failed       result.hide         result.ok           result.pty          result.return_code  result.shell        result.stderr       result.stdout	

关于c.run()命令:

Connection objects’ methods (like run) usually return instances of invoke.runners.Result (or subclasses thereof) exposing the sorts of details seen above: what was requested, what happened while the remote action occurred, and what the final result was.

													-----引用 http://docs.fabfile.org/en/2.1/getting-started.html

也就是 Connection 对象的方法(例如run) 常常返回invoke.runners.Result的实例,这个实例暴露了一些细节:

我们请求了什么,我们远程操作发生了什么,最终的结果又是什么。

以上是fabric的初步使用。

自动回复

当我们需要sudo操作的权限的时候,远程服务器会阻塞在那,直到我们输入密码,这肯定是不行的。如果这么low,那我们使用这个框架做啥子?

我们开始试验:(我centos是root权限,所以来个骚操作)

手动输入版本:

In [13]: c.run('ssh tly@192.168.11.11', pty=True)
The authenticity of host '192.168.11.11 (192.168.11.11)' can't be established.
ECDSA key fingerprint is SHA256:vDOg8wbz0RSFDPGJGEmMc6lT32eR13xW9NxOPxRO2t0.
ECDSA key fingerprint is MD5:f7:77:c8:bf:e0:ba:bd:8b:4d:48:6a:86:f0:3a:dc:31.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.11.11' (ECDSA) to the list of known hosts.
tly@192.168.11.11's password: 
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-130-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

350 packages can be updated.
0 updates are security updates.

Last login: Wed Jul 11 14:11:36 2018 from 192.200.41.46
tly@tly-dev:~$ exit
logout
Connection to 192.168.11.11 closed.
Out[13]: <Result cmd='ssh tly@192.168.11.11' exited=0>

pty=True,我的理解就是将远程终端的stdout输出到本地。如果不添加会报错。

上面,我们连接了192.168.11.111 ,然后又用ssh连接到了192.168.11.11(要不是测试,我想我脑子瓦特了),之后,命令返回了终端需要我们输入密码的字眼。我手动的输入密码,操作成功。

自动输入的版本:

In [14]: from invoke import Responder 

In [15]: sudopass = Responder(
   ....: pattern=r"'s password:",
   ....: response='1\n',
   ....: )

In [16]: c.run('ssh tly@192.168.11.11', pty=True, watchers=[sudopass])
tly@192.168.11.11's password: 
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-130-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

350 packages can be updated.
0 updates are security updates.

Last login: Wed Jul 11 15:11:53 2018 from 192.168.11.111
tly@tly-dev:~$ exit
logout
Connection to 192.168.11.11 closed.
Out[16]: <Result cmd='ssh tly@192.168.11.11' exited=0>

分析如下:

引入了invoke中的Responder库。这个库将用来匹配字符串,并自动回复。

从终端发回来的数据来看,我们阻塞的地方就是我们需要填写密码的地方。即:

tly@192.168.11.11's password:

所以在创建Responder对象的时候,匹配的字符串就选择“ 's password:” 来匹配。 

response参数也就是我们需要自动回复的文本。

sudo帮手

我远程终端是centos 根用户运行的。不需要root, 所以我切换到centos下使用fabric。

使用如下:

In [1]: import getpass
In [3]: sudo_pass = getpass.getpass("What's your sudo password?")
What's your sudo password?

In [5]: from fabric import Config

In [6]: from fabric import Connection

In [7]: sudo_pass = getpass.getpass("What's your sudo password?")
What's your sudo password?

In [8]: config = Config(overrides={'sudo': {'password': sudo_pass}})
In [11]: c = Connection('192.168.11.11', port=22, user='tly', config=config, connect_kwargs={'password':'1'})

In [12]: c.sudo('whoami', hide='stderr')
root
Out[12]: <Result cmd="sudo -S -p '[sudo] password: ' whoami" exited=0>

分析如下:

getpass 只是用来获取密码使用的。(私密处理了一下吧)

sudo_pass中就是你输入的文本值。

传输文件

远程部署的最常用的命令了吧。	(巨坑爹的来了)

命令如下:

In [20]: c
Out[20]: <Connection host=192.168.11.11 user=tly>

In [21]: result = c.put('mysql-rc.yaml', '/home/tly/mysql-rc.yaml')

只要使用put命令就能将文件推送过去了。 

参数:
  1. 为本机路径
  2. 为远程路径 现在这个版本一定要指定远程文件名!!!不然会报错 本机默认目录就是你执行脚本的目录。如果要切换怎么办? 暂时还没有找到比较方便的方式。所以先建议使用绝对路径。

如果只是在本地运行命令,可以使用

In [27]: from invoke import run

In [28]: run('ls')
anaconda-ks.cfg
mysql-rc.yaml
test_dir
Out[28]: <Result cmd='ls' exited=0>