fabric2.0 使用说明html
fabric框架,主要的目的就是用来远程自动化部署。在最近,做者将fabric框架重写了一遍,升级到了2.0版本。在我学习过程当中,遇到了很多的坑,最坑的一次就是python编译器总是给我提示,fabric 导入 api失败,没有fabric.api。我不断的怀疑本身是否是脑子瓦特了的时候,我pip list 查看了一下版本,再看了一下github版本。python
我谢谢做者呕心沥血的更新框架。mysql
本说明,结合官方文档,使用效果更佳。git
安装github
pip install fabric
复制代码
查看一下pip安装fabric的版本号:sql
fabric 2.1.3shell
一切OK,开始练习。ubuntu
初步的使用centos
准备两台机器。一个pc,一个虚机就行。个人是两个虚机,ip地址以下:api
虚机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中一些参数:
链接的一些其余参数都放到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命令就能将文件推送过去了。
参数:
复制代码
若是只是在本地运行命令,可使用
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>
复制代码