Authored by chunhua.zhang

自动生成inventory文件

@@ -3,68 +3,46 @@ @@ -3,68 +3,46 @@
3 10.66.0.3 3 10.66.0.3
4 4
5 5
6 -[java-gateway]  
7 -10.66.70.204  
8 -10.66.70.22  
9 -10.66.0.240  
10 -10.66.0.5  
11 -10.66.0.4  
12 -  
13 -[java-users]  
14 -10.66.70.128  
15 -10.66.70.9  
16 -  
17 -[java-resources]  
18 -10.66.70.128  
19 -10.66.70.9  
20 -  
21 -[java-order]  
22 -10.66.70.170  
23 -10.66.70.41  
24 6
25 [java-promotion] 7 [java-promotion]
26 -10.66.70.170  
27 10.66.70.41 8 10.66.70.41
  9 +10.66.70.170
28 10
29 11
30 -[java-product] 12 +[java-sns]
31 10.66.70.47 13 10.66.70.47
32 10.66.70.96 14 10.66.70.96
33 15
34 -[java-message]  
35 -10.66.70.47  
36 -10.66.70.96  
37 16
38 -[java-sns] 17 +[java-product]
39 10.66.70.47 18 10.66.70.47
40 10.66.70.96 19 10.66.70.96
41 20
42 21
  22 +[java-message]
  23 +10.66.70.100
  24 +10.66.70.109
43 25
44 -[java-brower]  
45 -10.66.70.42  
46 -10.66.70.223  
47 -  
48 -  
49 -  
50 -[java-wechat]  
51 -10.66.70.139  
52 -10.66.70.12  
53 26
  27 +[java-order]
  28 +10.66.70.41
  29 +10.66.70.170
54 30
55 -[java-activity]  
56 -10.66.70.89  
57 -10.66.70.142  
58 31
  32 +[java-gateway]
  33 +10.66.70.22
  34 +10.66.70.204
  35 +10.66.0.240
  36 +10.66.0.4
  37 +10.66.0.5
59 38
60 -[java-erpgateway]  
61 -10.66.50.140  
62 -10.66.50.113  
63 39
  40 +[java-users]
  41 +10.66.70.9
  42 +10.66.70.128
64 43
65 -[java-union]  
66 -10.66.70.21  
67 -10.66.70.49  
68 -10.66.103.20  
69 44
  45 +[java-resources]
  46 +10.66.70.9
  47 +10.66.70.128
70 48
  1 +[java-nginx]
  2 +10.66.0.2
  3 +10.66.0.3
  4 +
  5 +
  6 +
  7 +[java-promotion]
  8 +10.66.70.41
  9 +10.66.70.170
  10 +
  11 +
  12 +[java-sns]
  13 +10.66.70.47
  14 +10.66.70.96
  15 +
  16 +
  17 +[java-product]
  18 +10.66.70.47
  19 +10.66.70.96
  20 +
  21 +
  22 +[java-message]
  23 +10.66.70.100
  24 +10.66.70.109
  25 +
  26 +
  27 +[java-order]
  28 +10.66.70.41
  29 +10.66.70.170
  30 +
  31 +
  32 +[java-gateway]
  33 +10.66.70.22
  34 +10.66.70.204
  35 +10.66.0.240
  36 +10.66.0.4
  37 +10.66.0.5
  38 +
  39 +
  40 +[java-users]
  41 +10.66.70.9
  42 +10.66.70.128
  43 +
  44 +
  45 +[java-resources]
  46 +10.66.70.9
  47 +10.66.70.128
  48 +
@@ -4,36 +4,44 @@ @@ -4,36 +4,44 @@
4 4
5 5
6 6
7 -[java-gateway]  
8 -10.66.104.2  
9 -10.66.104.44  
10 -10.66.104.28  
11 -10.66.104.29 7 +[java-promotion]
  8 +10.66.104.16
  9 +10.66.104.41
  10 +
  11 +
  12 +[java-sns]
  13 +10.66.104.12
  14 +10.66.104.48
12 15
13 16
  17 +[java-product]
  18 +10.66.104.12
  19 +10.66.104.48
14 20
15 -[java-brower]  
16 -10.66.104.7  
17 -10.66.104.39  
18 21
  22 +[java-message]
  23 +10.66.104.12
  24 +10.66.104.48
19 25
20 26
21 -[java-wechat]  
22 -10.66.202.7  
23 -10.66.202.32 27 +[java-order]
  28 +10.66.104.16
  29 +10.66.104.41
24 30
25 31
26 -[java-activity]  
27 -10.66.104.7  
28 -10.66.104.39 32 +[java-gateway]
  33 +10.66.104.2
  34 +10.66.104.44
  35 +10.66.104.28
  36 +10.66.104.29
  37 +
29 38
  39 +[java-users]
  40 +10.66.104.10
  41 +10.66.104.43
30 42
31 -[java-erpgateway]  
32 -10.66.104.7  
33 -10.66.104.39  
34 43
  44 +[java-resources]
  45 +10.66.104.10
  46 +10.66.104.43
35 47
36 -[java-union]  
37 -10.66.70.21  
38 -10.66.70.49  
39 -10.66.103.20  
  1 +[java-nginx]
  2 +10.66.104.15
  3 +10.66.104.13
  4 +
  5 +
  6 +
  7 +[java-promotion]
  8 +10.66.104.16
  9 +10.66.104.41
  10 +
  11 +
  12 +[java-sns]
  13 +10.66.104.12
  14 +10.66.104.48
  15 +
  16 +
  17 +[java-product]
  18 +10.66.104.12
  19 +10.66.104.48
  20 +
  21 +
  22 +[java-message]
  23 +10.66.104.12
  24 +10.66.104.48
  25 +
  26 +
  27 +[java-order]
  28 +10.66.104.16
  29 +10.66.104.41
  30 +
  31 +
  32 +[java-gateway]
  33 +10.66.104.2
  34 +10.66.104.44
  35 +10.66.104.28
  36 +10.66.104.29
  37 +
  38 +
  39 +[java-users]
  40 +10.66.104.10
  41 +10.66.104.43
  42 +
  43 +
  44 +[java-resources]
  45 +10.66.104.10
  46 +10.66.104.43
  47 +
@@ -2,34 +2,44 @@ @@ -2,34 +2,44 @@
2 10.66.202.5 2 10.66.202.5
3 10.66.202.12 3 10.66.202.12
4 4
5 -[java-gateway]  
6 -10.66.202.15  
7 -10.66.202.13  
8 -10.66.202.8 5 +
  6 +[java-promotion]
  7 +10.66.202.16
  8 +10.66.202.47
  9 +
  10 +
  11 +[java-sns]
  12 +10.66.202.17
  13 +10.66.202.39
9 14
10 15
11 -[java-brower]  
12 -10.66.202.7  
13 -10.66.202.32 16 +[java-product]
  17 +10.66.202.17
  18 +10.66.202.39
14 19
15 20
  21 +[java-message]
  22 +10.66.202.17
  23 +10.66.202.39
16 24
17 -[java-wechat]  
18 -10.66.202.7  
19 -10.66.202.32  
20 25
  26 +[java-order]
  27 +10.66.202.16
  28 +10.66.202.47
  29 +
  30 +
  31 +[java-gateway]
  32 +10.66.202.13
  33 +10.66.202.15
  34 +10.66.202.8
21 35
22 -[java-activity]  
23 -10.66.202.14  
24 -10.66.202.41  
25 36
  37 +[java-users]
  38 +10.66.202.11
  39 +10.66.202.21
26 40
27 -[java-erpgateway]  
28 -10.66.202.7  
29 -10.66.202.32  
30 41
  42 +[java-resources]
  43 +10.66.202.11
  44 +10.66.202.21
31 45
32 -[java-union]  
33 -10.66.70.21  
34 -10.66.70.49  
35 -10.66.103.20  
  1 +[java-nginx]
  2 +10.66.202.5
  3 +10.66.202.12
  4 +
  5 +
  6 +[java-promotion]
  7 +10.66.202.16
  8 +10.66.202.47
  9 +
  10 +
  11 +[java-sns]
  12 +10.66.202.17
  13 +10.66.202.39
  14 +
  15 +
  16 +[java-product]
  17 +10.66.202.17
  18 +10.66.202.39
  19 +
  20 +
  21 +[java-message]
  22 +10.66.202.17
  23 +10.66.202.39
  24 +
  25 +
  26 +[java-order]
  27 +10.66.202.16
  28 +10.66.202.47
  29 +
  30 +
  31 +[java-gateway]
  32 +10.66.202.13
  33 +10.66.202.15
  34 +10.66.202.8
  35 +
  36 +
  37 +[java-users]
  38 +10.66.202.11
  39 +10.66.202.21
  40 +
  41 +
  42 +[java-resources]
  43 +10.66.202.11
  44 +10.66.202.21
  45 +
@@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
9 import json 9 import json
10 import os 10 import os
11 from subprocess import call 11 from subprocess import call
  12 +from jinja2 import Environment, PackageLoader
12 13
13 14
14 # clone deploy project to local 15 # clone deploy project to local
@@ -22,8 +23,8 @@ call("mkdir -p %s" % PATH, shell=True) @@ -22,8 +23,8 @@ call("mkdir -p %s" % PATH, shell=True)
22 call("git clone --recursive git@git.yoho.cn:yohoops/auto_deploy.git %s" % PATH, shell=True) 23 call("git clone --recursive git@git.yoho.cn:yohoops/auto_deploy.git %s" % PATH, shell=True)
23 24
24 25
25 -# 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)]  
26 -azs = ['qcloud', 'qcloudaz2', 'qcloudaz3'] 26 +# get iptables info from git files.
  27 +azs_mappings = {"qcloud": "az1", "qcloudaz2": "az2", "qcloudaz3": "az3"}
27 host_group = { "yoho_gateway": "java-gateway", 28 host_group = { "yoho_gateway": "java-gateway",
28 "yoho_users": "java-users", 29 "yoho_users": "java-users",
29 "yohobuy_resources": "java-resources", 30 "yohobuy_resources": "java-resources",
@@ -33,30 +34,44 @@ host_group = { "yoho_gateway": "java-gateway", @@ -33,30 +34,44 @@ host_group = { "yoho_gateway": "java-gateway",
33 "yoho_message": "java-message", 34 "yoho_message": "java-message",
34 "yoho_sns": "java-sns" 35 "yoho_sns": "java-sns"
35 } 36 }
36 -  
37 -for az in azs:  
38 -  
39 - fout = open(os.path.join(HOME, ".tmp", az), 'w')  
40 - fout.write("# inventory file generated by auto_deploy iptables\n")  
41 -  
42 - file = open(os.path.join(PATH, "scripts/iptable/", az, "app-iptable.sh"), 'r') 37 +# all available zone groups
  38 +all_groups = {}
  39 +for _az in azs_mappings.keys():
  40 + groups = {}
  41 + all_groups[azs_mappings[_az]] = groups
  42 + file = open(os.path.join(PATH, "scripts/iptable/", _az, "app-iptable.sh"), 'r')
43 for line in file: 43 for line in file:
44 - for g in host_group.keys():  
45 - if g in line:  
46 - fout.write("[%s]\n" % host_group[g]) 44 + for group_name in host_group.keys():
  45 + if group_name in line:
  46 + g_hosts = []
  47 + groups[host_group[group_name]] = g_hosts
47 i_1 = line.index("(") 48 i_1 = line.index("(")
48 i_2 = line.index(")") 49 i_2 = line.index(")")
49 sub = line[i_1 + 1 : i_2] 50 sub = line[i_1 + 1 : i_2]
50 for user_ip in sub.split(" "): 51 for user_ip in sub.split(" "):
51 - ip = user_ip[user_ip.index("@") + 1 : ].strip()  
52 - fout.write("%s\n" % ip) 52 + g_hosts.append(user_ip[user_ip.index("@") + 1 : ].strip())
  53 +
53 54
54 file.close() 55 file.close()
55 - fout.close()  
56 - print("success write ansible inventory hosts files to %s " % (os.path.join(HOME, ".tmp", az)))  
57 56
  57 +print("get all inventory info success. \n")
  58 +print(all_groups)
58 59
59 -print("success to generate ansible inventory files") 60 +## generate az inventory files
  61 +env = Environment(loader=PackageLoader('inventory'))
  62 +
  63 +for _az in azs_mappings.keys():
  64 + az = azs_mappings[_az]
  65 +
  66 + #backup old file and open new file
  67 + hosts = os.path.join("./inventories", az, "hosts")
  68 + call("mv %s %s" % (hosts, os.path.join("./inventories", az, "hosts.bak")), shell=True)
  69 + file = open(hosts, 'w')
  70 +
  71 + #write template
  72 + template = env.get_template('inventory-%s.j2' % az)
  73 + file.write(template.render(groups = all_groups[az]))
  74 + file.close()
60 75
61 76
62 77
No preview for this file type
  1 +[java-nginx]
  2 +10.66.0.2
  3 +10.66.0.3
  4 +
  5 +
  6 +{% extends "java-services.j2" %}
  1 +[java-nginx]
  2 +10.66.104.15
  3 +10.66.104.13
  4 +
  5 +
  6 +{% extends "java-services.j2" %}
  1 +[java-nginx]
  2 +10.66.202.5
  3 +10.66.202.12
  4 +
  5 +{% extends "java-services.j2" %}
  1 +{% for group, hosts in groups.iteritems() %}
  2 +[{{ group }}]
  3 +{% for host in hosts -%}
  4 +{{ host }}
  5 +{% endfor %}
  6 +{% endfor %}