author: headsen  chen

date: 2018-08-12  23:13:16

1,安装

yum -y install epel-release
yum -y install fabric

2,指定密码的使用:
fab -p 123456 -H 192.168.10.10 -f f1.py w

#cat f1.py

#!/usr/bin/env python
from fabric.api import run
def w():
run('w')

确定:当指定的密码不正确的时候,会一直让你尝试输入密码

3,通过定义的密码文件来执行fab命令

[root@localhost mnt]# vim f2.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from fabric.api import *
env.hosts = ['192.168.10.104','192.168.10.105','192.168.10.101']
env.port = ''
env.user = 'root'
env.password = '' def a():
with cd('/tmp'):
run('touch a{1..10}')
run('ls /tmp')
def b():
run('uptime')
@task
def go():
a()
b()

[root@localhost mnt]# fab -f f2.py go

[192.168.10.104] Executing task 'go'
[192.168.10.104] run: touch a{1..10}
[192.168.10.104] run: ls /tmp
[192.168.10.104] out: a1 a10 a2 a3 a4 a5 a6 a7 a8 a9 yum.log
[192.168.10.104] out: [192.168.10.104] run: uptime
[192.168.10.104] out: 05:21:33 up 57 min, 3 users, load average: 0.00, 0.00, 0.00
[192.168.10.104] out: [192.168.10.105]
...
[192.168.10.101]
...
Done.
Disconnecting from 192.168.10.101... done.
Disconnecting from 192.168.10.104... done.
Disconnecting from 192.168.10.105... done.

4,指定秘钥对连接(将该密钥对的公钥放到对方的机器的 /root/.ssh/id_pub_rsa文件中去)

[root@localhost mnt]# cat bak-com.py
#!/usr/bin/env python
from fabric.api import * def w():
run("w")
[root@localhost mnt]# fab  -i /root/.ssh/id_rsa -H 192.168.8.222 -f bak-com.py w
[192.168.8.222] Executing task 'w'
[192.168.8.222] run: w
[192.168.8.222] out: :: up days, :, users, load average: 0.16, 0.15, 0.10
[192.168.8.222] out: USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
[192.168.8.222] out: root pts/ 192.168.20.29 : : .28s .04s -bash
[192.168.8.222] out: root pts/ 192.168.20.29 : .00s .01s .01s w
[192.168.8.222] out:
Done.
Disconnecting from 192.168.8.222... done.

4,多个客户端的不同端口和不同密码的模板

[root@localhost mnt]# cat f4.py

#!/usr/bin/env python
# coding:utf-8
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import * env.hosts=['root@192.168.10.104:22','root@192.168.10.105:22','root@192.168.10.101:22']
# -----> 这一步当都为root和22端口时可以简写为:env.hosts=['192.168.10.104','192.168.10.105','192.168.10.101']
env.user = 'root'
env.passwords = {
'root@192.168.10.104:22':'', # ------->注意:这里的为固定格式,22端口指定不可少,否则报错
'root@192.168.10.105:22':'',
'root@192.168.10.101:22':'jack123',
}
def a():
with cd('/tmp'):
run('touch a{1..10}')
def b():
run('uptime')
@task
def c():
a()
b()

5,利用fabric来传送本地文件到客户端上去

[root@localhost mnt]# vim f6.py

#!/usr/bin/env python
# coding:utf-8
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import *
env.hosts=['192.168.10.104','192.168.10.105','192.168.10.101']
env.user = 'root'
env.passwords = {
'root@192.168.10.104:22':'',
'root@192.168.10.105:22':'',
'root@192.168.10.101:22':'jack123',
}
def a():
put('/tmp/jack123','/tmp/') # --------> 此时在客户端上生成 jack123的文件,如果要在客户度上改名:put('/tmp/jack123','/tmp/jack')

[root@localhost mnt]# fab -f f6.py a

[192.168.10.104] Executing task 'a'
[192.168.10.104] put: /tmp/jack123 -> /tmp/jack123
[192.168.10.105] Executing task 'a'
[192.168.10.105] put: /tmp/jack123 -> /tmp/jack123
[192.168.10.101] Executing task 'a'
[192.168.10.101] put: /tmp/jack123 -> /tmp/jack123 Done.
Disconnecting from 192.168.10.101... done.
Disconnecting from 192.168.10.104... done.
Disconnecting from 192.168.10.105... done.

6,从客户端上拉取文件到本地

[root@localhost mnt]# cat f7.py

#!/usr/bin/env python
# coding:utf-8
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import *
env.host=['192.168.10.104']
env.user = 'root'
env.password = 'jack123'
def a():
get('/tmp/tom','/opt/')

[root@localhost mnt]# fab -f f7.py a

[192.168.10.104] Executing task 'a'
[192.168.10.104] download: /opt/tom <- /tmp/tom
Disconnecting from 192.168.10.104... done.

7,task装饰器的作用

被标识的函数为fab可调用的,程序内的其他函数不可通过fab -f x.py go 调用,当整个程序内都没有@task时,则任何函数都可以被
fab命令调用

实例:

#!/usr/bin/env python
from fabric.api import *
def lsfab():
with lcd('/mnt/'):
local('ls')
def host_name():
local('uptime')
@task
def go():
lsfab()
host_name()

此时不可以通过:fab -f f1.py lsfab 此时不成功。当没有@task时,这样调用可以

 8,run_once

标识的函数只会执行一次,只会在第一台主机执行,后面的主机不再执行其下的函数,不受多台主机影响。

实例:
[root@localhost mnt]# cat f3.py

#!/usr/bin/env python
# coding:utf-8
from fabric.api import * env.hosts=['192.168.13.128','192.168.13.130']
env.user='root'
env.password=''
def a():
with cd('/tmp'):
run('ls')
def b():
run('uptime')
@runs_once
def c():
a()
b()

[root@localhost mnt]# fab -f f3.py c # 此时在13.130机器上就不执行了。

[root@192.168.13.128:10000] Executing task 'c'
[root@192.168.13.128:10000] run: ls
[root@192.168.13.128:10000] out: yum.log [root@192.168.13.128:10000] run: uptime
[root@192.168.13.128:10000] out: 01:59:19 up 2:21, 3 users, load average: 0.00, 0.00, 0.00 Done.
Disconnecting from 192.168.13.128:10000... done.
[root@localhost mnt]#

9,fab的本地执行和远程执行

[root@localhost 7]# cat f2.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from fabric.api import *
env.user='root'
env.hosts=['192.168.13.128','192.168.13.129']
env.password='' @runs_once
def local_task () : #本地任务函数
local("uname -a") def remote_task():
with cd ("/home") : # “with”的作用是让后面的表达式的语句继承当前状态,实现
run ("ls -1") # “cd /data/logs && Is -1"的效果

10,交互式fab使用方法(执行命令时输入参数)

[root@localhost 7]# cat f3.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from fabric.api import *
env.user='root'
env.hosts=['192.168.13.128','192.168.13.129']
env.password='' @runs_once #主机遍历过程中,只有第一台触发此函数
def input_raw():
return prompt("please input directory name:",default="/home") def worktask(dirname):
run("ls -l "+dirname)
@task #限定只有go函数对fab命令可见
def go():
getdirname = input_raw()
worktask(getdirname)

[root@localhost mnt]# fab -f f8.py go

[192.168.10.104] Executing task 'go'
please input directory name: [/home] /tmp
[192.168.10.104] run: ls -l /tmp
[192.168.10.104] out: total 12
[192.168.10.104] out: -rw-r--r-- 1 root root 0 Aug 13 05:45 a1
... [192.168.10.105] Executing task 'go'
[192.168.10.105] run: ls -l /tmp
[192.168.10.105] out: total 8
[192.168.10.105] out: -rw-r--r-- 1 root root 0 Aug 13 05:45 a1
...
[192.168.10.105] out: Done.
Disconnecting from 192.168.10.104... done.
Disconnecting from 192.168.10.105... done.

fabric入门的更多相关文章

  1. Hyperledger Fabric 入门 first-network 搭建

    1.准备环境: 安装git.docker.curl.go [root@test_vonedao_83 fabric]# git --version git version 1.8.3.1 [root@ ...

  2. fabric 更详尽的用法

    项目发布和运维的工作相当机械,频率还蛮高,导致时间浪费在敲大量重复的命令上. 修复bug什么的,测试,提交版本库(2分钟),ssh到测试环境pull部署(2分钟),rsync到线上机器A,B,C,D, ...

  3. HyperLedger Fabric 学习思路分享

    HyperLedger Fabric 学习思路分享 HyperLedger Fabric最初是由Digital Asset和IBM公司贡献的.由Linux基金会主办的一个超级账本项目,它是一个目前非常 ...

  4. fabric 自动化部署

    fabric 项目发布和运维的工作相当机械,频率还蛮高,导致时间浪费在敲大量重复的命令上. 修复bug什么的,测试,提交版本库(2分钟),ssh到测试环境pull部署(2分钟),rsync到线上机器A ...

  5. 从零使用Python测试。Testing Using Python.

    0. 写在前面 本人使用Python测试已有多年,略有些皮毛经验.每次有新员工入职,都会从头教一遍如何入门上手使用Python进行测试.趁这段有空,整理成文档,也好方便后续新员工学习.文章如有不妥之处 ...

  6. Linux 自动化部署

    1.pexpect Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Py ...

  7. Python学习笔记—自动化部署【新手必学】

      前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:唯恋殊雨   目录 pexpect fabric pexpect P ...

  8. django开发环境搭建(参考流程)

    django开发环境搭建(参考流程) 2013-08-08 01:09:06 分类: LINUX 原文地址:django开发环境搭建(参考流程) 作者:bailiangcn 对于一个初学者,在实际的开 ...

  9. Linux上天之路(十八)之自动化部署

    pexpect Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Pyth ...

随机推荐

  1. 网络配置vlan

    1. # This file describes the network interfaces available on your system # and how to activate them. ...

  2. [oracle] 两种权限:系统权限VS对象权限

    系统权限表示对表和表空间等   有无操作权  的权限.一般是SYS用户这种DBA来授权.比如: grant create session to lisi grant create table to l ...

  3. php 不依赖数据实现删除图片,核心代码

    <?php $file = "ueditor\php\upload\image\*\*.png"; foreach (glob("$file") as $ ...

  4. C# 使用IrisSkin2.dll皮肤库C# ssk皮肤

    其实皮肤就是一个第三方的控件,名字是IrisSkin2.dll只要添加到你的工具箱里就可以和其它控件一样使用了下面我说一下使用的方法,不对的地方大家多指教啊. 一.添加控件IrisSkin2.dll. ...

  5. Java中关于“=”和“==”的分析

    Java中变量分为普通原始变量(int char float等)和对象 一"=" (1)普通原始变量 普通原始变量的声明和赋值语句例如以下 int a=3; int b=a; 此时 ...

  6. Unity5.4新版AssetBundle资源打包

    (1)新版本 唯一打包API Buildpipeline.BuildAssetBundle (2)在资源的Inpector界面最下方可设置该资源的assetbundleName, 每个assetbun ...

  7. 实操演练!MathType几个绝妙小技巧!

    在论文中编写公式时MathType绝对是很多人不二的选择,它的功能比较完善,操作比较方便,包含的符号模板很多,易学易上手,这些都是它的优点.但是在使用MathType时,还有很多绝妙的小技巧,使用起来 ...

  8. sublime text 2 破解

    本文是介绍sublime text 2.0.2 build 2221 64位 的破解 在你使用sublime时可能经常出现下图: 这是在提醒你注册 在工具栏上点击help->Enter Lice ...

  9. Trie树(字典树)(1)

    Trie树.又称字典树,单词查找树或者前缀树,是一种用于高速检索的多叉树结构. Trie树与二叉搜索树不同,键不是直接保存在节点中,而是由节点在树中的位置决定. 一个节点的全部子孙都有同样的前缀(pr ...

  10. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...