在集群环境中,有时候需要批量修改nginx配置,或批量添加vhost主机。手动一个个添加,效率太慢,借助Fabric写了一个批量分发的脚本,会提高不少效率。
思路:
(1.在一台nginx修改配置或添加vhost主机,并测试;
(2.测试成功后,将配制文件推送至堡垒机(跳板机);
(3.在堡垒机上将配置文件分发至其他nginx服务器 [如果原有文件存在,则先备份] ;
只是在堡垒机环境下一个简单的推送文件脚本,比较简陋,代码如下:
# -*- coding:utf-8 -*- #! /usr/bin/env python from fabric.api import * from fabric.colors import * from fabric.context_managers import * from fabric.contrib.console import confirm import time env.port= ‘22‘ env.user=‘root‘ env.password=‘123456‘ env.roledefs = { ‘get-server‘: [‘192.168.64.128‘,], ‘put-seever‘: [‘192.168.64.131‘,], } env.project_nginx_conf = ‘/soft/nginx.conf‘ env.deploy_nginx_conf = ‘/usr/local/nginx/conf/nginx.conf‘ @task @roles(‘get-server‘) def get_file(): print yellow("Start get configuration file ...") result = get(env.deploy_nginx_conf,env.project_nginx_conf) if result.failed and no("get file failed, Continue[Y/N]?"): abort("Aborting file get task!") print green("get to configuration files successfully!") @task @roles(‘put-seever‘) def put_file(): print yellow("Start put configuration file ...") run("cp -r %s %s_%s.back" % (env.deploy_nginx_conf,env.deploy_nginx_conf,time.strftime("%Y%m%d"))) result = put(env.project_nginx_conf,env.deploy_nginx_conf) if result.failed and no("put file failed, Continue[Y/N]?"): abort("Aborting file put task!") print green("put to configuration files successfully!") @task def start(): execute(get_file) execute(put_file)
本文出自 “ˉ、穎濤┃﹎” 博客,请务必保留此出处http://hypocritical.blog.51cto.com/3388028/1685256
原文地址:http://hypocritical.blog.51cto.com/3388028/1685256