Fabric-Python知名批量自动化部署/测试模块

Fabric Python知名开源自动化部署模块Fabric,强烈推荐给各位!(运维,测试…均可以用它实现无比具有想象力的任务)他主要用于对设备/服务器批量执行任务(同时更换500台服务器的密码、同时更新或者重启500台虚拟机)安装和使用也极其方便,没有什么中间代码,没有架构,没有内幕!
  或许对于批量自动化部署而言,这是一个很大很大的福音。
  我们来看一个VPSee提供的案例:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from fabric.api import *
import string
from random import choice
import socket
import paramiko
env.user = 'root'
env.password = 'root'
env.hosts = [ 'grid00', 'grid01', 'grid02', 'grid03', 'grid04', 'grid05']
@task
@parallel
def passwd(user, passwd=False):
with settings(hide('running', 'stdout', 'stderr'), warn_only=True):
if isup(env.host):
if not passwd:
passwd = genpass()
sudo("echo -e '%s\n%s' | passwd %s" % (passwd, passwd, user))
def genpass(length=10):
return ''.join(choice(string.ascii_letters + string.digits) for _ in range(length))
def isup(host):
print 'connecting host: %s' % host
timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(1)
up = True
try:
paramiko.Transport((host, 22))
except Exception, e:
up = False
print '%s down, %s' % (host, e)
finally:
socket.setdefaulttimeout(timeout)
return up

最新内容请见作者的GitHub页:http://qaseven.github.io/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值