-
Notifications
You must be signed in to change notification settings - Fork 14
/
mnet2ansible.py
45 lines (39 loc) · 1.15 KB
/
mnet2ansible.py
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
# Loads Data Discovered from MNET Suite and writes an Ansible Host File
#Example
#[discovered]
#10.10.10.10
#10.10.10.11
#10.10.10.11
#
# Example Mnet output -
# "hostname","10.10.10.10","Model","12.1(22)EA10a","Serial","","IOS"
import sys, getopt
import csv
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'Arguments: -i <MNET CSV File> -o <Ansible Output File>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'Arguments -i <MNET CSV File> -o <Ansible Output File>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Processing MNET Data from:', inputfile
print 'Ansible Host File:', outputfile
mnet_file = open(inputfile)
rd = csv.reader(mnet_file)
hosts = []
with open(outputfile,'w+') as ansible_file:
writer = csv.writer(ansible_file)
writer.writerow(['[discovered]'])
for host in rd:
writer.writerow([str(host[1])])
if __name__ == "__main__":
main(sys.argv[1:])