Fabric的使用Notes

1. 安装fabric == 1.14.0 版本

2. 初步使用

从hello_world开始:

    def hello(name="world"):
    print("hello %s"%name)

在命令行下直接执行:

fab -f xxx.py hello:name=Miroslav Klose
输出:
hello Miroslav Klose

Done.

fab 是fabric的一个shell命令用来执行函数, -f 指定是哪个文件,默认是fabfile.py

3. 函数的使用


3.1、local

local函数:本地执行的shell命令

from fabric.api import local

def prepare_deploy():
local('uname -s')

执行如下

$ fab -f fabfile_2.py prepare_deploy
[localhost] local: uname -s
Linux

Done.


3.2、run

远程服务器执行命令

run('ls -l')


3.3、cd

def execute_ls():
    with cd('/etc'):
        run('uname -s')
        run('ls -l')

进入文件夹,且保存上下文环境。一定要使用with


3.4、lcd

切换本地文件夹,操作如上


3.5、put

上传本地文件到远程服务器

def execute_put():
    with cd('/root'):
        put('/home/Klose/project/fibric_demo/xxx', 'xxx')

结果如下:

$ fab -f fabfile_3.py execute_put
[root@192.168.11.231:22] Executing task 'execute_put'
[root@192.168.11.231:22] put: /home/Klose/project/fibric_demo/xxx -> /root/xxx

Done.
Disconnecting from root@192.168.11.231... done.


3.6、get

下载本地文件到远程服务器

def execute_get():
    get('/root/xxx', '%(path)s')

结果如下:

$ fab -f fabfile_3.py execute_get
[root@192.168.11.233:22] Executing task 'execute_get'
[root@192.168.11.233:22] download: /home/Klose/project/fibric_demo/xxx <- /root/xxx

Done.

4. 环境的使用

需要访问远程服务器的话,那么我们需要填入一些基本的信息,host,user,password等信息。

fabric中是通过设置env上下文管理器来管理的。

如下:

from fabric.api import *

env.hosts = ['192.168.11.233','192.168.11.232']  #添加多台服务器的域名
env.user = 'root'       #服务器的用户名
env.password = '1'      #服务器的密码

def execute_ls():
    with cd('/virus'):
        run('ls')
        run('uname -s')

执行如下:

(fabric) Klose@Miroslav Klose:~/project/fibric_demo$ fab -f fabfile_3.py execute_ls
[192.168.11.233] Executing task 'execute_ls'
[192.168.11.233] run: ls
[192.168.11.233] out: anaconda-ks.cfg  api.tar  docker  perl5  saas-setup  saas-setup.tar.gz

[192.168.11.233] run: uname -s
[192.168.11.233] out: Linux

[192.168.11.232] Executing task 'execute_ls'
[192.168.11.232] run: ls
[192.168.11.232] out: anaconda-ks.cfg  authority.tar    zabbix-release-3.4-2.el7.noarch.rpm

[192.168.11.232] run: uname -s
[192.168.11.232] out: Linux


Done.
Disconnecting from 192.168.11.233... done.
Disconnecting from 192.168.11.232... done.

如果多个服务器,有多个不同的命令该怎么写?

使用env.passwords来配置。代码如下:

from fabric.api import *

env.hosts = ['192.168.11.233', '192.168.11.231']
env.user = 'root'
env.passwords = {
    'root@192.168.11.233:22' : '1',
    'root@192.168.11.231:22' : '1'
}

def execute_ls():
    run('uname -s')
    run('ls -l')

一定要写端口等信息;

5. 分配不同的组

多个项目,不同的操作,那么我们就需要使用不同的操作。引入 分组

from fabric.api import *

env.passwords = {
    'root@192.168.11.233:22' : '1',
    'root@192.168.11.231:22' : '1',
    'root@192.168.11.203:22' : '2',
    'Klose@192.168.11.11:22': 1,
}

env.roledefs = {
            'testserver': ['root@192.200.41.233:22',],  
            'realserver': ['Klose@192.168.11.11:22', ]
            }

@roles('testserver')
def execute_ls():
    with cd('/etc'):
        run('uname -s')
        run('ls -l')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值