fabric框架,主要的目的就是用来远程自动化部署。在最近,作者将fabric框架重写了一遍,升级到了2.0版本。在我学习过程中,遇到了不少的坑,最坑的一次就是python编译器老是给我提示,fabric 导入 api失败,没有fabric.api。我不断的怀疑自己是不是脑子瓦特了的时候,我pip list 查看了一下版本,再看了一下github版本。
我谢谢作者呕心沥血的更新框架。
本说明,结合官方文档,使用效果更佳。
安装
1
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)上进行。。。
1 2 3 4 5
from fabric import Connection In [3]: c = Connection('192.168.11.111', port=22, user='root', connect_kwargs={'password':'1'})
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.
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 1114:11:362018from192.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>