febric的使用
1 from fabric.api import * 2 from fabric.colors import * 3 from fabric.tasks import execute 4 ''' 5 env.hosts = ['192.468.4.50','192.168.4.51'] 6 env.user = 'root' 7 env.port = 22 8 env.password = '123456' 9 env.passwords = { 10 'root@192.168.4.50:22':'123456', 11 'root@192.168.4.51:22':'123456', 12 } 13 ''' 14 15 16 env.hosts = [ 17 'root@192.168.4.50:22', 18 'root@192.168.4.51:22', 19 'root@192.168.4.52:22', 20 'root@192.168.4.53:22', 21 'root@192.168.4.54:22', 22 'root@192.168.4.55:22', 23 'root@192.168.4.56:22', 24 'root@192.168.4.57:22', 25 'root@192.168.4.58:22', 26 ] 27 env.password = '123456' 28 29 30 @runs_once 31 def xxx(): 32 cmd = prompt('请输入要执行的命令:') 33 run(cmd) 34 35 36 # 本地执行命令 37 @runs_once 38 def local_cmd(cmd, dir=None): 39 if dir == None: 40 dir ='/root' 41 with settings(hide('everything'), warn_only=True): 42 with lcd(dir): # 本地切换目录 43 result = local(cmd) # 本地执行命令 44 print(green(result)) 45 46 # 远程执行命令 47 def remote_cmd(cmd, dir=None): 48 if dir == None: 49 dir = '/root' 50 with settings(hide('everything'), warn_only=True): 51 with cd(dir): # 远程切换目录 52 result= run(cmd) # 远程执行命令 53 print(green(result)) 54 55 # 查看端口信息 56 def port_info(port): 57 cmd = "ss -autnlp | grep {}".format(port) 58 with settings(hide('everything'), warn_only=True): 59 result = run(cmd) 60 print(green(result)) 61 62 63 64 # 安装软件 65 def install_soft(soft): 66 cmd = "yum -y install %s"%soft 67 with settings(hide('everything'), warn_only=True): 68 run(cmd) 69 cmd = "rpm -qa | grep %s"%soft 70 if cmd: 71 print(green('安装成功')) 72 73 # 重启服务 74 def restart(service): 75 cmd = "systemctl restart {}".format(service) 76 with settings(hide('everything'), warn_only=True): 77 result = run(cmd) 78 print(green(result)) 79 80 # 停止服务 81 def stop(service): 82 cmd = "systemctl stop {}".format(service) 83 with settings(hide('everything'), warn_only=True): 84 result = run(cmd) 85 print(green(result)) 86 87 # 查看服务状态 88 def status(service): 89 cmd = "systemctl status {}".format(service) 90 with settings(hide('everything'), warn_only=True): 91 result = run(cmd) 92 print(green(result)) 93 http://127.0.0.1:8000/ 94 # 上传文件/压缩文件解压 95 def upload(src, tar=None): 96 if tar == None: 97 tar = src 98 with settings(hide('everything'), warn_only=True): 99 put(src, tar) 100 print(green('upload successful')) 101 print(tar[-3:]) 102 if tar[-3:] == ".gz": 103 cmd = "tar -xf %s" % tar 104 run(cmd) 105 106 107 # 下载文件 108 def download(src, tar=None): 109 if tar == None: 110 tar = src 111 with settings(hide('everything'), warn_only=True): 112 get(src, tar) 113 print(green('download successful')) 114 115 116 117 118 #if __name__ == '__main__': 119 # execute(xxx) 120 # fab -f fabfile.py update 121 # fab -f fabfile.py port_status:80
from fabric.api import * env.user = 'root' env.hosts = [ '192.168.4.50', '192.168.4.51', ] env.password = '123456' @runs_once # 只执行一次 @task def local_update(dir): with lcd(dir): # 切换到本地某目录下 local("git add -A") local("git commit -m update") local("git pull origin master") local("git push origin master") @task def remote_update(dir): with cd(dir): # 切换到远程某目录 run("git checkout") run("git pull origin master") @task def deploy(): local_update() remote_update()
from fabric.api import * from fabric.context_managers import * from fabric.contrib.console import confirm env.user = 'root' env.gateway = '192.168.1.23' #定义堡垒机IP,作为文件上传、执行的中转设置 env.hosts = ['192.168.1.21','192.168.1.22'] env.passwords = { 'root@192.168.1.21:22':'123456', 'root@192.168.1.22:22':'abcdef', 'root@192.168.1.23:22':'123abc', #堡垒机账号信息 } lpackpath = '/home/install/lnmp.tar.gz' #本地安装包路径 rpackpath = '/tmp/install' #远程安装包路径 @task def put_task(): #上传文件 run('mkdir -p /tmp/install') #默认情况下,当命令执行失败时,Fabric会停止执行后续命令。有时,我们允许忽略失败的命令继续执行,比如run(‘rm /tmp/abc')在文件不存在的时候有可能失败,这时可以用with settings(warn_only=True):执行命令,这样Fabric只会打出警告信息而不会中断执行。 with settings(warn_only=True): result = put(lpackpath,rpackpath) #上传 if result.failed and not confirm('put file failed,Continue[Y/N]?'): abort('Aborting file put task!') @task def run_task(): #安装 with cd('/tmp/install'): run('tar -zxvf lnmp.tar.gz') with cd('lnmp/'): #使用with继承/tmp/install目录位置状态 run('./centos.sh') @task def go(): #上传、安装组合命令 put_task() run_task()