fabric 学习笔记
fabric安装
目前,从PyPI可以搜索到主要的fabric库为“ Fabric 2.1.3 ”、“ fabric2 2.1.3 ”和“ Fabric3 1.14.post1 ”。
Fabric:官方Fabric,兼容 Python 2 & Python 3,但不兼容Fabric 1.x的fabfile;
fabric2: 与Fabric相同,仅作为平滑迁移(使用Fabric包安装1.x 版本,使用Fabric2包安装2.x版本,来实现1.x和2.x的共存);
Fabric3:是一个基于Fabric 1.x 的fork,兼容Python2 & Python3,兼容 Fabric1.x 的 fabfile;
1、centos安装fabric3 1.14 post1
- sudo yum install python34.x86_64
- python3.4 -m venv fabric-env
- source fabric-env/bin/activate
- pip install --upgrade pip
- pip install fabric3
- deactivate
2、 ubuntu 安装fabric3
- apt-get update
- apt install python3-pip
- pip3 install virtualenv
- virtualenv learn-virtualenv
- source fabric-env/bin/activate
- pip install fabric3
- deactivate
3、windows安装
- ##下载get-pip.py
- ## https://pip.pypa.io/en/stable/installing/
- #安装pip
- C:\Users\wongg>python get-pip.py
- Collecting pip
- #安装虚拟环境程序
- C:\Users\wongg>pip install virtualenv
- Collecting virtualenv
- Successfully installed virtualenv-15.2.
- #创建虚拟环境
- C:\Users\wongg>virtualenv cmdb-virtualenv
- #进入虚拟环境
- C:\Users\wongg>cmdb-virtualenv\Scripts\activate
4、检验安装
- (fabric-env) # fab -V
- Fabric3 1.14.post1
- Paramiko 2.4.
- C:\Users\wongg\learn>fab -V
- Fabric3 1.14.post1
- Paramiko 2.4.
- $ pip show fabric3
- Name: Fabric3
- Version: 1.14.post1
- Summary: Fabric is a simple, Pythonic tool for remote execution and deployment (py2.7/py3.4+ compatible fork).
- Home-page: https://github.com/mathiasertl/fabric/
- Author: Jeff Forcier
- Author-email: jeff@bitprophet.org
- License: UNKNOWN
- Location: /home/huangjunfa/fabric-env/lib/python2.7/site-packages
- Requires: paramiko, six
- Required-by:
- $ python -c "from fabric.api import env ; print (env.version)"
- 1.14.post1
5、常用设置
import 设置
- import sys,os
- from fabric.api import env,local,cd,lcd,run,task,execute
用户密码设置一
- env.hosts = ['10.10.3.2','10.10.0.10']
- env.user = 'test'
- env.password ="fabric20180703"
rsa设置一
- env.hosts = ['10.10.3.2','10.10.0.10']
- env.user = 'test'
env.key_filename = os.path.join(sys.path[0],'rsa','huangjunfa.key')
用户密码分别设置二
- env.hosts = ['user111@10.10.0.10','user222@10.10.3.2']
- env.passwords ={'user111@10.10.0.10:22':'fabric20180703','user222@10.10.3.2:22':'fabric20180703'}
rsa用户分别设置二
- env.hosts = ['user111@10.10.0.10','user222@10.10.3.2']
- env.key_filename = os.path.join(sys.path[0],'rsa','id_rsa.key')
6、直接调用命令执行
- # @task
- def local_cmd():
- local('ls -la')
- with lcd('/mnt'):
- local('ls -l')
- #@task
- def remote_cmd():
- #run('free -m')
- #run('cat /etc/passwd')
- print('host_string:%s\n'%env.host_string)
- with cd(''):
- run('ls')
- print('host_string:%s\n'%env.passwords)
- # run('du -h')
- out=run('ps -ef |grep "/usr/sbin/httpd"|grep -v grep|wc -l')
- print('result:%s \n'%(out))
- if out != 0:
- print('application is running!!')
- if __name__ == '__main__':
- #execute(remote_cmd,host="10.10.3.2")
- execute(remote_cmd)
- ##按顺序在每台host上执行remote_cmd
环境设置官方文档:
http://fabric-chs.readthedocs.io/zh_CN/chs/usage/env.html
fab命令运行调用过程
- Traceback (most recent call last):
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 478, in connect
- client.connect(**kwargs)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\client.py", line 424, in connect
- passphrase,
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\client.py", line 714, in _auth
- raise saved_exception
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\client.py", line 701, in _auth
- self._transport.auth_password(username, password)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\transport.py", line 1381, in auth_password
- return self.auth_handler.wait_for_response(my_event)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\auth_handler.py", line 226, in wait_for_response
- raise e
- paramiko.ssh_exception.AuthenticationException: Authentication failed.
- During handling of the above exception, another exception occurred:
- Traceback (most recent call last):
- File "C:\Users\wongg\learn\tt.py", line 49, in <module>
- execute(remote_cmd)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\tasks.py", line 387, in execute
- multiprocessing
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\tasks.py", line 277, in _execute
- return task.run(*args, **kwargs)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\tasks.py", line 174, in run
- return self.wrapped(*args, **kwargs)
- File "C:\Users\wongg\learn\tt.py", line 36, in remote_cmd
- run('ls')
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 692, in host_prompting_wrapper
- return func(*args, **kwargs)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\operations.py", line 1095, in run
- shell_escape=shell_escape, capture_buffer_size=capture_buffer_size,
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\operations.py", line 935, in _run_command
- channel=default_channel(), command=wrapped_command, pty=pty,
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\state.py", line 435, in default_channel
- chan = _open_session()
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\state.py", line 416, in _open_session
- transport = connections[env.host_string].get_transport()
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 156, in __getitem__
- self.connect(key)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 148, in connect
- user, host, port, cache=self, seek_gateway=seek_gateway)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 566, in connect
- password = prompt_for_password(text)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 651, in prompt_for_password
- new_password = _password_prompt(password_prompt, stream)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 623, in _password_prompt
- return getpass.getpass(prompt, stream)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\getpass.py", line 101, in win_getpass
- return fallback_getpass(prompt, stream)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\getpass.py", line 127, in fallback_getpass
- return _raw_input(prompt, stream)
- File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\getpass.py", line 147, in _raw_input
- line = input.readline()
- KeyboardInterrupt
fabric 学习笔记的更多相关文章
- fabric私密数据学习笔记
fabric私密数据学习笔记 私密数据分为两部分 一个是真正的key,value,它被存在 peer的私密数据库(private state)中. 另一部分为公共数据,它是真实的私密数据key,val ...
- 雨痕 的《Python学习笔记》--附脑图(转)
原文:http://www.pythoner.com/148.html 近日,在某微博上看到有人推荐了 雨痕 的<Python学习笔记>,从github上下载下来看了下,确实很不错. 注意 ...
- HyperLedger Cello学习笔记
HyperLedger Cello学习笔记 转载请注明出处:HyperLedger Cello学习笔记 概述 Hyperledger Cello是Hyperledger下的一个子项目,其主要功能如下: ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- seaJs学习笔记2 – seaJs组建库的使用
原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...
随机推荐
- 20145331魏澍琛《网络对抗》Exp8 Web基础
20145331魏澍琛<网络对抗>Exp8 Web基础 实践内容: 1.简单的web前端页面(HTML.CSS等) 2.简单的web后台数据处理(PHP) 3.Mysql数据库 4.一个简 ...
- 20165211 2017-2018-2 《Java程序设计》第5周学习总结
20165211 2017-2018-2 <Java程序设计>第5周学习总结 教材学习内容总结 本周,我学习了书本上第五.六两章的内容,以下是我整理的主要知识. 第五章 内部类与异常类 内 ...
- 第五章 消息摘要算法--MAC
注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第6章“验证数据完整性--消息摘要算法” 5.1.mac(又称为Hmac) 原理:在md与sha系列算法的基础上加入了密钥,是 ...
- Ubuntu 下 su:authentication failure的解决办法
Ubuntu下使用 su 切换到超级用户时候遇到下面的问题 su: Authentication failure 解决办法: $ sudo passwd root Enter new UNIX pas ...
- UVa 11300 分金币
https://vjudge.net/problem/UVA-11300 题意: 圆桌上有n个人,每个人都有一定的初始金币,每个人可以给他旁边的人一些金币,最终使每个人的金币数相等.计算最少需要转手的 ...
- 学习mybatis-3 step by step 篇二
Mapper XML 文件 基本的*Mapper.xml文件配置就不熬述了具体可参考: http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html 1.sq ...
- BZOJ4896 [Thu Summer Camp2016]补退选
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Vim简本
参考链接:http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/ 本文将更简化,只保留其中的精华部分. Level One — ...
- python 时间戳转元组
#!/usr/bin/python # -*- coding: UTF- -*- import time localtime = time.localtime(time.time()) print(& ...
- ubuntu server 16.04(amd 64) 配置网桥,多网卡使用激活
安装了Ubuntu16.04的server版本,结果进入系统输入ifconfig后发现,只有一个网卡enp1s0,还有一个网络回路lo,ifconfig -a 发现其实一共有四个网卡,enp1s0,e ...