利用Python在堡垒机模式下批量管理后端nginx服务器
在集群环境中,有时候需要批量修改nginx配置,或批量添加vhost主机。手动一个个添加,效率太慢,借助Fabric写了一个批量分发的脚本,会提高不少效率。 思路:(1.在一台nginx修改配置或添加vhost主机,并测试;
(2.测试成功后,将配制文件推送至堡垒机(跳板机);
(3.在堡垒机上将配置文件分发至其他nginx服务器 [如果原有文件存在,则先备份] ;
只是在堡垒机环境下一个简单的推送文件脚本,比较简陋,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- 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?"):
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?"):
abort("Aborting file put task!")
print green("put to configuration files successfully!")
@task
def start():
execute(get_file)
execute(put_file)
页:
[1]