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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#!/usr/bin/python
# -*- coding:utf-8 -*-
###############################################################################
#Author: arvon
#Email: yafeng2011@126.com
#Blog: http://arvon.top/
#Date: 2016/08/04
#Filename: excel_write_inventory.py
#Revision: 1.1
#License: GPL
#Description: use xlrd module auto create vars and file for ansible
#Notes:
###############################################################################
from xlrd import open_workbook
#input vars
excel_file = 'test.xlsx'
#input the excel file path
inventory_path = './'
#input the inventory file dir path
ops_dir_path = 'you/ansible/path'
#input the ops-repo dir path
inventory_file = 'inventory.yml'
#input the ansible's inventory filename
inventory_name = 'ssy'
#define vars
data = open_workbook(excel_file)
table = data.sheets()[0]
#open the excel first sheets
nrows = table.nrows
#nrows is the tables line numbers
#functions
#write title to inventory file
def write_title():
f = open(inventory_path + inventory_file, 'w')
input_msg = \
'''\
hosts_ops_path: /data/ops-repo/
inventory:
name: ssy
hosts:
'''
f.write('host_ops_path: ' + ops_dir_path + '\n')
f.write('inventory:' + '\n')
f.write(' name: ' + inventory_name + '\n')
f.write(' hosts:' + '\n')
f.close
#write all hosts
def write_hosts(row_num,host_name,group_name,host_num = 1):
for each_line_num in range(nrows):
each_line = table.row_values(each_line_num)
if each_line[row_num] == 1:
f = open(inventory_path + inventory_file, 'a')
f.write(' - name: '+ host_name + str(host_num) + '\n')
host_num = host_num + 1
f.write(' ip: ' + str(each_line[1]) + '\n')
f.write(' group: '+ group_name + '\n')
f.write("\n")
f.close
#use functions
write_title()
write_hosts(4,'db','dbserver')
write_hosts(5,'redis','redis')
write_hosts(6,'zk','zookeeper')
write_hosts(7,'kafka','kafka')
write_hosts(8,'mysql','mysql')
write_hosts(9,'rest','rest')
write_hosts(10,'thrift','thrift')
write_hosts(11,'push','push')
write_hosts(12,'db-ejabberd','ejabberd-db')
write_hosts(13,'conn-ejabberd','ejabberd-conn')
write_hosts(14,'nginx','nginx')
write_hosts(15,'web','web')
write_hosts(16,'turn','turn')
write_hosts(17,'media','media')
write_hosts(18,'coference','coference')
|