Fabric的官方网站:

http://www.fabfile.org

帮助文档:

https://fabric-chs.readthedocs.io/zh_CN/chs/tutorial.html

  1. Fabric的安装
yum install -y make gcc gcc++ python-devel python-pip
$ pip install fabric==1.14.0
或者
$ pip3 install fabric2 (没有fabric.api模块)
  1. 验证安装
python
>>> import fabric
或者
python3
>>> import fabric
  1. 命令行入口fab
fab [options]  --  [shell command]

Fabric的核心API

核心API 类别
带颜色的输出类 color output
上下文管理类 context managers
装饰类 decorators
网络类 network
操作类 operations
任务类 tasks
工具类 utils

fabric.api命令集

  1. 使用方法
$ cat fabfile.py
----------------------------------
#!/usr/bin/python
# -*- coding: utf-8 -*- from fabric.api import *
from fabric.colors import *
from fabric.context_managers import * env.user = 'beeworkshop'
env.hosts = ['192.168.30.66']
env.password = '123456' @task
def run_remote_cmds():
print yellow("我要执行命令啦")
with settings(warn_only=True): <---错误也继续执行
local("hostname")
local("uname -a")
run("w")
run("hostname")
run("ifconfig")
run("ls -l")
---------------------------------
$ fab -l
Available commands: run_remote_cmds $ fab run_remote_cmds
[192.168.30.66] Executing task 'run_remote_cmds'
我要执行命令啦
[localhost] local: hostname
bee-a
[localhost] local: uname -a
Linux bee-a 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[192.168.30.66] run: w
[192.168.30.66] out: 01:04:50 up 1:35, 1 user, load average: 0.00, 0.00, 0.00
[192.168.30.66] out: USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
[192.168.30.66] out: beeworks pts/0 192.168.30.6 01:04 0.00s 0.02s 0.02s w
[192.168.30.66] out: [192.168.30.66] run: hostname
[192.168.30.66] out: sdn-testbed
[192.168.30.66] out: [192.168.30.66] run: ifconfig
[192.168.30.66] out: ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
[192.168.30.66] out: inet 192.168.74.128 netmask 255.255.255.0 broadcast 192.168.74.255
[192.168.30.66] out: inet6 fe80::4dec:63ce:223b:a7cf prefixlen 64 scopeid 0x20<link>
[192.168.30.66] out: ether 00:0c:29:68:44:26 txqueuelen 1000 (Ethernet)
[192.168.30.66] out: RX packets 89251 bytes 115992608 (115.9 MB)
[192.168.30.66] out: RX errors 0 dropped 0 overruns 0 frame 0
[192.168.30.66] out: TX packets 27312 bytes 1844389 (1.8 MB)
[192.168.30.66] out: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[192.168.30.66] out:
[192.168.30.66] out: ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
[192.168.30.66] out: inet 192.168.30.66 netmask 255.255.255.0 broadcast 192.168.30.255
[192.168.30.66] out: inet6 fe80::6094:b573:8d2f:dd5 prefixlen 64 scopeid 0x20<link>
[192.168.30.66] out: ether 00:0c:29:68:44:30 txqueuelen 1000 (Ethernet)
[192.168.30.66] out: RX packets 305 bytes 39864 (39.8 KB)
[192.168.30.66] out: RX errors 0 dropped 0 overruns 0 frame 0
[192.168.30.66] out: TX packets 230 bytes 38470 (38.4 KB)
[192.168.30.66] out: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[192.168.30.66] out:
[192.168.30.66] out: lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
[192.168.30.66] out: inet 127.0.0.1 netmask 255.0.0.0
[192.168.30.66] out: inet6 ::1 prefixlen 128 scopeid 0x10<host>
[192.168.30.66] out: loop txqueuelen 1000 (Local Loopback)
[192.168.30.66] out: RX packets 373 bytes 28853 (28.8 KB)
[192.168.30.66] out: RX errors 0 dropped 0 overruns 0 frame 0
[192.168.30.66] out: TX packets 373 bytes 28853 (28.8 KB)
[192.168.30.66] out: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[192.168.30.66] out:
[192.168.30.66] out: [192.168.30.66] run: ls -l
[192.168.30.66] out: total 48
[192.168.30.66] out: drwxrwxr-x 2 beeworkshop beeworkshop 4096 4月 24 21:20 bak
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月 24 19:40 Desktop
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月 24 19:40 Documents
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月 24 19:40 Downloads
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月 24 19:40 Music
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月 24 19:40 Pictures
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月 24 19:40 Public
[192.168.30.66] out: drwxrwxr-x 5 beeworkshop beeworkshop 4096 4月 25 01:09 sdnsoftware
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月 24 19:40 Templates
[192.168.30.66] out: drwxrwxr-t 2 beeworkshop beeworkshop 4096 4月 25 18:23 thinclient_drives
[192.168.30.66] out: drwxrwxr-x 3 beeworkshop beeworkshop 4096 4月 24 23:33 tools
[192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4月 24 19:40 Videos
[192.168.30.66] out: Done.
Disconnecting from 192.168.30.66... done.

如果不是默认的文件fabfile.py,则必须用参数-f指出:

fab -f ~/test.py run_remote_cmds

指出默认的task。此时fab后边可以不必给出函数名,直接fab即可。

@task(default=True)

注意:\cmd 可以取消别名的作用。

例子1

#!/usr/bin/python
# -*- coding: utf-8 -*- from fabric.api import *
from fabric.colors import *
from fabric.context_managers import * env.user = 'beeworkshop'
env.hosts = ['192.168.30.66','192.168.30.67','192.168.30.68','192.168.30.69']
env.password = '123456' @task
def put_hosts_files():
print yellow("rsync /etc/host file")
with settings(warn_only=true):
put("/etc/hosts","/etc/hosts")
print green("rsync file success") for host in env.hosts
env.host_string = host
put_hosts_files()

这里@task装饰的函数可以多个,然后全部放在for循环中。fab会根据env.host_string的当前值逐个运行函数。

Fabric管理组件的使用的更多相关文章

  1. Agile.Net 组件式开发平台 - 脚本管理组件

    脚本管理组件用于管理系统查询脚本,由于数据查询的复杂性和可变性,平台规范要求使用查询使用建立在脚本管理器中的SQL语句.新增.更新.删除数据采用数据访问支持库API. 示例如下: 首先在脚本管理器中定 ...

  2. Agile.Net 组件式开发平台 - 内核管理组件

    敏捷开发体系   软件构件技术:所谓软件构件化,就是要让软件开发像机械制造工业一样,可以用各种标准和非标准的零件来进行组装.软件的构件化和集成技术的目标是:软件系统可以由不同厂商提供的,用不同语言开发 ...

  3. Prism 文档 第三章 管理组件之间的依赖关系

                                                                          第3章:管理组件之间的依赖关系 基于Prism库的复合应用程 ...

  4. vue30-单一事件管理组件通信: vuex

    ------------------------------------------------------ 可以单一事件管理组件通信: vuex var Event=new Vue(); Event ...

  5. 纯粹极简的react状态管理组件unstated

    简介 unstated是一个极简的状态管理组件 看它的简介:State so simple, it goes without saying 对比 对比redux: 更加灵活(相对的缺点是缺少规则,需要 ...

  6. Unity2D项目-平台、解谜、战斗! 1.5 Player框架、技能管理组件

    各位看官老爷们,这里是RuaiRuai工作室,一个做单机游戏的兴趣作坊. 前文提到,凡是有"攻击"语义的对象,在游戏中,我们给予其一个"CanFight"组件予 ...

  7. 使用 Composer 为 ThinkPHP(3.2.3)框架添加和管理组件

    环境:Windows 64位 PHP 版本: 框架:ThinkPHP Tips: 组件:打包的代码,可以是一系列相关的类(class).接口(interface).特性(trait),用于解决某个具体 ...

  8. vuejs单一事件管理组件间的通信

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Nodejs mongodb 管理组件adminmongodb

    强大的 nodejs的mongodb管理工具,强大到即下即用: 安装需求: 1.git命令获取组件包,git clone https://github.com/mrvautin/adminMongo. ...

随机推荐

  1. 21.Nodejs基础知识(下)——2019年12月16日

    2019年10月04日16:56:23 7. 模块 7.1 暴露一个类,字段 var bar = require("./bar.js"); var msg = "你好&q ...

  2. Pyhton爬虫实战

    Pyhton爬虫实战 零.致谢 感谢BOSS直聘相对权威的招聘信息,使本人有了这次比较有意思的研究之旅. 由于爬虫持续爬取 www.zhipin.com 网站,以致产生的服务器压力,本人深感歉意,并没 ...

  3. navicat的使用(测试库和正式库同步)以及用plsql改表字段属性

    说明:数据库的操作,除了查询,最好先做好备份,比如数据同步.更新.修改或删除之类的: netstat -antp   查看mysql端口 firewall -cmd --list-all    查看防 ...

  4. 20180802-Java 方法

    Java 方法 下面的方法包含2个参数num1和num2,它返回这两个参数的最大值. /** 返回两个整型变量数据的较大值**/ public static int max(int num1,int ...

  5. (转)Python3 zip() 函数

    转:http://www.runoob.com/python3/python3-func-zip.html 描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返 ...

  6. Spring源码解读--(一)源码下载

    走在Java程序员这条路上,网上Java各种工具满天飞,写个简单的CRUD,相信是个开发都能写出来,于是在思考如何可以在同行业中更有竞争力(其实就是如何赚更多钱).那么,老大给我推荐了Spring源码 ...

  7. php面试专题---8、会话控制考点

    php面试专题---8.会话控制考点 一.总结 一句话总结: 主要是cookie和session的区别,以及用户禁用cookie之后怎么使用session 1.为什么要使用会话控制技术? 因为http ...

  8. linux设备驱动第四篇:从如何定位oops的代码行谈驱动调试方法

    上一篇我们大概聊了如何写一个简单的字符设备驱动,我们不是神,写代码肯定会出现问题,我们需要在编写代码的过程中不断调试.在普通的c应用程序中,我们经常使用printf来输出信息,或者使用gdb来调试程序 ...

  9. machine learning 之 Recommender Systems

    整理自Andrew Ng的machine learning 课程 week 9. 目录: Problem Formulation(问题的形式) Content Based Recommendation ...

  10. .Net-WCF-图书:《WCF编程》

    ylbtech-.Net-WCF-图书:<WCF编程> <WCF编程>是2008年1月机械工业出版社出版的图书,作者是Juval Lowy.Clemens Vasters. 1 ...