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

  1. sudo yum install python34.x86_64 
  2.  
  3. python3.4 -m venv fabric-env
  4.  
  5. source fabric-env/bin/activate
  6.  
  7. pip install --upgrade pip
  8.  
  9. pip install fabric3
  10.  
  11. deactivate

2、 ubuntu 安装fabric3

  1. apt-get update
  2.  
  3. apt install python3-pip
  4.  
  5. pip3 install virtualenv
  6.  
  7. virtualenv learn-virtualenv
  8.  
  9. source fabric-env/bin/activate
  10.  
  11. pip install fabric3
  12.  
  13. deactivate

3、windows安装

  1. ##下载get-pip.py
  2. ## https://pip.pypa.io/en/stable/installing/
  3.  
  4. #安装pip
  5. C:\Users\wongg>python get-pip.py
  6. Collecting pip
  7.  
  8. #安装虚拟环境程序
  9. C:\Users\wongg>pip install virtualenv
  10. Collecting virtualenv
  11. Successfully installed virtualenv-15.2.
  12.  
  13. #创建虚拟环境
  14. C:\Users\wongg>virtualenv cmdb-virtualenv
  15.  
  16. #进入虚拟环境
  17. C:\Users\wongg>cmdb-virtualenv\Scripts\activate

4、检验安装

  1. (fabric-env) # fab -V
  2. Fabric3 1.14.post1
  3. Paramiko 2.4.
  1. C:\Users\wongg\learn>fab -V
  2. Fabric3 1.14.post1
  3. Paramiko 2.4.
  1. $ pip show fabric3
  2. Name: Fabric3
  3. Version: 1.14.post1
  4. Summary: Fabric is a simple, Pythonic tool for remote execution and deployment (py2.7/py3.4+ compatible fork).
  5. Home-page: https://github.com/mathiasertl/fabric/
  6. Author: Jeff Forcier
  7. Author-email: jeff@bitprophet.org
  8. License: UNKNOWN
  9. Location: /home/huangjunfa/fabric-env/lib/python2.7/site-packages
  10. Requires: paramiko, six
  11. Required-by:
  12.  
  13. $ python -c "from fabric.api import env ; print (env.version)"
  14. 1.14.post1

5、常用设置

import 设置

  1. import sys,os
  2. from fabric.api import env,local,cd,lcd,run,task,execute

用户密码设置一

  1. env.hosts = ['10.10.3.2','10.10.0.10']
  2. env.user = 'test'
  3. env.password ="fabric20180703"

rsa设置一

  1. env.hosts = ['10.10.3.2','10.10.0.10']
  2. env.user = 'test'
    env.key_filename = os.path.join(sys.path[0],'rsa','huangjunfa.key')

用户密码分别设置二

  1. env.hosts = ['user111@10.10.0.10','user222@10.10.3.2']
  2. env.passwords ={'user111@10.10.0.10:22':'fabric20180703','user222@10.10.3.2:22':'fabric20180703'}

rsa用户分别设置二

  1. env.hosts = ['user111@10.10.0.10','user222@10.10.3.2']
  2. env.key_filename = os.path.join(sys.path[0],'rsa','id_rsa.key')

6、直接调用命令执行

  1. # @task
  2. def local_cmd():
  3. local('ls -la')
  4. with lcd('/mnt'):
  5. local('ls -l')
  6.  
  7. #@task
  8. def remote_cmd():
  9. #run('free -m')
  10. #run('cat /etc/passwd')
  11. print('host_string:%s\n'%env.host_string)
  12. with cd(''):
  13. run('ls')
  14. print('host_string:%s\n'%env.passwords)
  15. # run('du -h')
  16. out=run('ps -ef |grep "/usr/sbin/httpd"|grep -v grep|wc -l')
  17. print('result:%s \n'%(out))
  18. if out != 0:
  19. print('application is running!!')
  20.  
  21. if __name__ == '__main__':
  22. #execute(remote_cmd,host="10.10.3.2")
  23. execute(remote_cmd)
  24. ##按顺序在每台host上执行remote_cmd

环境设置官方文档:

http://fabric-chs.readthedocs.io/zh_CN/chs/usage/env.html

fab命令运行调用过程

  1. Traceback (most recent call last):
  2. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 478, in connect
  3. client.connect(**kwargs)
  4. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\client.py", line 424, in connect
  5. passphrase,
  6. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\client.py", line 714, in _auth
  7. raise saved_exception
  8. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\client.py", line 701, in _auth
  9. self._transport.auth_password(username, password)
  10. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\transport.py", line 1381, in auth_password
  11. return self.auth_handler.wait_for_response(my_event)
  12. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\auth_handler.py", line 226, in wait_for_response
  13. raise e
  14. paramiko.ssh_exception.AuthenticationException: Authentication failed.
  15.  
  16. During handling of the above exception, another exception occurred:
  17.  
  18. Traceback (most recent call last):
  19. File "C:\Users\wongg\learn\tt.py", line 49, in <module>
  20. execute(remote_cmd)
  21. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\tasks.py", line 387, in execute
  22. multiprocessing
  23. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\tasks.py", line 277, in _execute
  24. return task.run(*args, **kwargs)
  25. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\tasks.py", line 174, in run
  26. return self.wrapped(*args, **kwargs)
  27. File "C:\Users\wongg\learn\tt.py", line 36, in remote_cmd
  28. run('ls')
  29. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 692, in host_prompting_wrapper
  30. return func(*args, **kwargs)
  31. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\operations.py", line 1095, in run
  32. shell_escape=shell_escape, capture_buffer_size=capture_buffer_size,
  33. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\operations.py", line 935, in _run_command
  34. channel=default_channel(), command=wrapped_command, pty=pty,
  35. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\state.py", line 435, in default_channel
  36. chan = _open_session()
  37. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\state.py", line 416, in _open_session
  38. transport = connections[env.host_string].get_transport()
  39. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 156, in __getitem__
  40. self.connect(key)
  41. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 148, in connect
  42. user, host, port, cache=self, seek_gateway=seek_gateway)
  43. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 566, in connect
  44. password = prompt_for_password(text)
  45. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 651, in prompt_for_password
  46. new_password = _password_prompt(password_prompt, stream)
  47. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\site-packages\fabric\network.py", line 623, in _password_prompt
  48. return getpass.getpass(prompt, stream)
  49. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\getpass.py", line 101, in win_getpass
  50. return fallback_getpass(prompt, stream)
  51. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\getpass.py", line 127, in fallback_getpass
  52. return _raw_input(prompt, stream)
  53. File "C:\Users\wongg\AppData\Local\Programs\Python\Python36\lib\getpass.py", line 147, in _raw_input
  54. line = input.readline()
  55. KeyboardInterrupt

fabric 学习笔记的更多相关文章

  1. fabric私密数据学习笔记

    fabric私密数据学习笔记 私密数据分为两部分 一个是真正的key,value,它被存在 peer的私密数据库(private state)中. 另一部分为公共数据,它是真实的私密数据key,val ...

  2. 雨痕 的《Python学习笔记》--附脑图(转)

    原文:http://www.pythoner.com/148.html 近日,在某微博上看到有人推荐了 雨痕 的<Python学习笔记>,从github上下载下来看了下,确实很不错. 注意 ...

  3. HyperLedger Cello学习笔记

    HyperLedger Cello学习笔记 转载请注明出处:HyperLedger Cello学习笔记 概述 Hyperledger Cello是Hyperledger下的一个子项目,其主要功能如下: ...

  4. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  5. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  6. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  7. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

  8. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  9. seaJs学习笔记2 – seaJs组建库的使用

    原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...

随机推荐

  1. 20145331魏澍琛《网络对抗》Exp8 Web基础

    20145331魏澍琛<网络对抗>Exp8 Web基础 实践内容: 1.简单的web前端页面(HTML.CSS等) 2.简单的web后台数据处理(PHP) 3.Mysql数据库 4.一个简 ...

  2. 20165211 2017-2018-2 《Java程序设计》第5周学习总结

    20165211 2017-2018-2 <Java程序设计>第5周学习总结 教材学习内容总结 本周,我学习了书本上第五.六两章的内容,以下是我整理的主要知识. 第五章 内部类与异常类 内 ...

  3. 第五章 消息摘要算法--MAC

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第6章“验证数据完整性--消息摘要算法” 5.1.mac(又称为Hmac) 原理:在md与sha系列算法的基础上加入了密钥,是 ...

  4. Ubuntu 下 su:authentication failure的解决办法

    Ubuntu下使用 su 切换到超级用户时候遇到下面的问题 su: Authentication failure 解决办法: $ sudo passwd root Enter new UNIX pas ...

  5. UVa 11300 分金币

    https://vjudge.net/problem/UVA-11300 题意: 圆桌上有n个人,每个人都有一定的初始金币,每个人可以给他旁边的人一些金币,最终使每个人的金币数相等.计算最少需要转手的 ...

  6. 学习mybatis-3 step by step 篇二

    Mapper XML 文件 基本的*Mapper.xml文件配置就不熬述了具体可参考: http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html 1.sq ...

  7. BZOJ4896 [Thu Summer Camp2016]补退选

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. Vim简本

    参考链接:http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/ 本文将更简化,只保留其中的精华部分. Level One — ...

  9. python 时间戳转元组

    #!/usr/bin/python # -*- coding: UTF- -*- import time localtime = time.localtime(time.time()) print(& ...

  10. ubuntu server 16.04(amd 64) 配置网桥,多网卡使用激活

    安装了Ubuntu16.04的server版本,结果进入系统输入ifconfig后发现,只有一个网卡enp1s0,还有一个网络回路lo,ifconfig -a 发现其实一共有四个网卡,enp1s0,e ...