Authored by chunhua.zhang

自动生成inventory文件

... ... @@ -3,68 +3,46 @@
10.66.0.3
[java-gateway]
10.66.70.204
10.66.70.22
10.66.0.240
10.66.0.5
10.66.0.4
[java-users]
10.66.70.128
10.66.70.9
[java-resources]
10.66.70.128
10.66.70.9
[java-order]
10.66.70.170
10.66.70.41
[java-promotion]
10.66.70.170
10.66.70.41
10.66.70.170
[java-product]
[java-sns]
10.66.70.47
10.66.70.96
[java-message]
10.66.70.47
10.66.70.96
[java-sns]
[java-product]
10.66.70.47
10.66.70.96
[java-message]
10.66.70.100
10.66.70.109
[java-brower]
10.66.70.42
10.66.70.223
[java-wechat]
10.66.70.139
10.66.70.12
[java-order]
10.66.70.41
10.66.70.170
[java-activity]
10.66.70.89
10.66.70.142
[java-gateway]
10.66.70.22
10.66.70.204
10.66.0.240
10.66.0.4
10.66.0.5
[java-erpgateway]
10.66.50.140
10.66.50.113
[java-users]
10.66.70.9
10.66.70.128
[java-union]
10.66.70.21
10.66.70.49
10.66.103.20
[java-resources]
10.66.70.9
10.66.70.128
... ...
[java-nginx]
10.66.0.2
10.66.0.3
[java-promotion]
10.66.70.41
10.66.70.170
[java-sns]
10.66.70.47
10.66.70.96
[java-product]
10.66.70.47
10.66.70.96
[java-message]
10.66.70.100
10.66.70.109
[java-order]
10.66.70.41
10.66.70.170
[java-gateway]
10.66.70.22
10.66.70.204
10.66.0.240
10.66.0.4
10.66.0.5
[java-users]
10.66.70.9
10.66.70.128
[java-resources]
10.66.70.9
10.66.70.128
... ...
... ... @@ -4,36 +4,44 @@
[java-gateway]
10.66.104.2
10.66.104.44
10.66.104.28
10.66.104.29
[java-promotion]
10.66.104.16
10.66.104.41
[java-sns]
10.66.104.12
10.66.104.48
[java-product]
10.66.104.12
10.66.104.48
[java-brower]
10.66.104.7
10.66.104.39
[java-message]
10.66.104.12
10.66.104.48
[java-wechat]
10.66.202.7
10.66.202.32
[java-order]
10.66.104.16
10.66.104.41
[java-activity]
10.66.104.7
10.66.104.39
[java-gateway]
10.66.104.2
10.66.104.44
10.66.104.28
10.66.104.29
[java-users]
10.66.104.10
10.66.104.43
[java-erpgateway]
10.66.104.7
10.66.104.39
[java-resources]
10.66.104.10
10.66.104.43
[java-union]
10.66.70.21
10.66.70.49
10.66.103.20
\ No newline at end of file
... ...
[java-nginx]
10.66.104.15
10.66.104.13
[java-promotion]
10.66.104.16
10.66.104.41
[java-sns]
10.66.104.12
10.66.104.48
[java-product]
10.66.104.12
10.66.104.48
[java-message]
10.66.104.12
10.66.104.48
[java-order]
10.66.104.16
10.66.104.41
[java-gateway]
10.66.104.2
10.66.104.44
10.66.104.28
10.66.104.29
[java-users]
10.66.104.10
10.66.104.43
[java-resources]
10.66.104.10
10.66.104.43
... ...
... ... @@ -2,34 +2,44 @@
10.66.202.5
10.66.202.12
[java-gateway]
10.66.202.15
10.66.202.13
10.66.202.8
[java-promotion]
10.66.202.16
10.66.202.47
[java-sns]
10.66.202.17
10.66.202.39
[java-brower]
10.66.202.7
10.66.202.32
[java-product]
10.66.202.17
10.66.202.39
[java-message]
10.66.202.17
10.66.202.39
[java-wechat]
10.66.202.7
10.66.202.32
[java-order]
10.66.202.16
10.66.202.47
[java-gateway]
10.66.202.13
10.66.202.15
10.66.202.8
[java-activity]
10.66.202.14
10.66.202.41
[java-users]
10.66.202.11
10.66.202.21
[java-erpgateway]
10.66.202.7
10.66.202.32
[java-resources]
10.66.202.11
10.66.202.21
[java-union]
10.66.70.21
10.66.70.49
10.66.103.20
\ No newline at end of file
... ...
[java-nginx]
10.66.202.5
10.66.202.12
[java-promotion]
10.66.202.16
10.66.202.47
[java-sns]
10.66.202.17
10.66.202.39
[java-product]
10.66.202.17
10.66.202.39
[java-message]
10.66.202.17
10.66.202.39
[java-order]
10.66.202.16
10.66.202.47
[java-gateway]
10.66.202.13
10.66.202.15
10.66.202.8
[java-users]
10.66.202.11
10.66.202.21
[java-resources]
10.66.202.11
10.66.202.21
... ...
... ... @@ -9,6 +9,7 @@
import json
import os
from subprocess import call
from jinja2 import Environment, PackageLoader
# clone deploy project to local
... ... @@ -22,8 +23,8 @@ call("mkdir -p %s" % PATH, shell=True)
call("git clone --recursive git@git.yoho.cn:yohoops/auto_deploy.git %s" % PATH, shell=True)
# 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']
# get iptables info from git files.
azs_mappings = {"qcloud": "az1", "qcloudaz2": "az2", "qcloudaz3": "az3"}
host_group = { "yoho_gateway": "java-gateway",
"yoho_users": "java-users",
"yohobuy_resources": "java-resources",
... ... @@ -33,30 +34,44 @@ host_group = { "yoho_gateway": "java-gateway",
"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')
# all available zone groups
all_groups = {}
for _az in azs_mappings.keys():
groups = {}
all_groups[azs_mappings[_az]] = groups
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])
for group_name in host_group.keys():
if group_name in line:
g_hosts = []
groups[host_group[group_name]] = g_hosts
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)
g_hosts.append(user_ip[user_ip.index("@") + 1 : ].strip())
file.close()
fout.close()
print("success write ansible inventory hosts files to %s " % (os.path.join(HOME, ".tmp", az)))
print("get all inventory info success. \n")
print(all_groups)
print("success to generate ansible inventory files")
## generate az inventory files
env = Environment(loader=PackageLoader('inventory'))
for _az in azs_mappings.keys():
az = azs_mappings[_az]
#backup old file and open new file
hosts = os.path.join("./inventories", az, "hosts")
call("mv %s %s" % (hosts, os.path.join("./inventories", az, "hosts.bak")), shell=True)
file = open(hosts, 'w')
#write template
template = env.get_template('inventory-%s.j2' % az)
file.write(template.render(groups = all_groups[az]))
file.close()
... ...
No preview for this file type
[java-nginx]
10.66.0.2
10.66.0.3
{% extends "java-services.j2" %}
\ No newline at end of file
... ...
[java-nginx]
10.66.104.15
10.66.104.13
{% extends "java-services.j2" %}
\ No newline at end of file
... ...
[java-nginx]
10.66.202.5
10.66.202.12
{% extends "java-services.j2" %}
\ No newline at end of file
... ...
{% for group, hosts in groups.iteritems() %}
[{{ group }}]
{% for host in hosts -%}
{{ host }}
{% endfor %}
{% endfor %}
\ No newline at end of file
... ...