title: Ansible根据excel自动生成inventory文件 date: 2016-08-05 tags: [Python,Ansible]

Tip: 根据excel表格手动写入inventory文件是个费时费力还容易出错的事情,这种事情本就应该使用脚本完成,刚好最近需要,然后就查阅文档,本来是想用shell的,毕竟相比python我还是对shell比较熟悉,奈何python的xlrd模块实在太合适所以就用python写了脚本(需要先安装xlrd模块)。 Update:现在不推荐使用xlrd模块了,原因是现在python3.0趋向于使用新模块openpyx,而且xlrd模块只是读取excel还需xlwd模块进行写入操作,不过现在这个模块已经支持不是很好了,我用pip没装上,所以现在使用openpyx模块,在我博客Jumpserver这篇文章里记录有这个模块的用法

安装xlrd模块

1
2
pip install xlrd
#想源码安装参照https://pypi.python.org/pypi/xlrd/  

Python脚本

 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')