Authored by chunhua.zhang

自动生成inventory 文件

#!/usr/bin/env python
# coding=utf-8
#!/usr/bin/python
# -*- coding: UTF-8 -*-
""""
generate ansible inventory files from auto-deploy: http://git.yoho.cn/yohoops/auto_deploy/tree/master/scripts/iptable
... ... @@ -10,19 +10,53 @@ import json
import os
from subprocess import call
PATH = "~/.tmp/auto_deploy"
# clone deploy project to local
HOME = os.path.expanduser("~")
PATH = os.path.join(HOME, ".tmp/auto_deploy")
if os.path.isdir(PATH):
call("rm -rf %s" % PATH , shell=True)
call("echo 'start to remove local git files'", shell=True)
call("rm -rf %s" % PATH, shell=True)
call("mkdir -p %s" % PATH, shell=True)
call("git clone --recursive git@git.yoho.cn:yohoops/auto_deploy.git %s" % PATH, shell=True)
print("success write ansible inventory hosts files to file: [inventory_hosts]")
# get iptables info from git files, parse file format: [yoho_gateway_qcloud_ips=(master@10.66.70.22 master@10.66.70.204 master@10.66.0.240 master@10.66.0.4 master@10.66.0.5)]
azs = ['qcloud', 'qcloudaz2', 'qcloudaz3']
host_group = { "yoho_gateway": "java-gateway",
"yoho_users": "java-users",
"yohobuy_resources": "java-resources",
"yohobuy_order" : "java-order",
"yohobuy_promotion": "java-promotion",
"yohobuy_product": "java-product",
"yoho_message": "java-message",
"yoho_sns": "java-sns"
}
for az in azs:
fout = open(os.path.join(HOME, ".tmp", az), 'w')
fout.write("# inventory file generated by auto_deploy iptables\n")
file = open(os.path.join(PATH, "scripts/iptable/", az, "app-iptable.sh"), 'r')
for line in file:
for g in host_group.keys():
if g in line:
fout.write("[%s]\n" % host_group[g])
i_1 = line.index("(")
i_2 = line.index(")")
sub = line[i_1 + 1 : i_2]
for user_ip in sub.split(" "):
ip = user_ip[user_ip.index("@") + 1 : ].strip()
fout.write("%s\n" % ip)
file.close()
fout.close()
print("success write ansible inventory hosts files to %s " % (os.path.join(HOME, ".tmp", az)))
print("success to generate ansible inventory files")
... ...