-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvBKUP.py
155 lines (124 loc) · 4.88 KB
/
invBKUP.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import json
import csv
import os
if os.path.exists("servers.txt"):
os.remove ("servers.txt")
else:
print("File does not exist.")
if os.path.exists("inventory.csv"):
os.remove ("inventory.csv")
else:
print("File does not exist.")
import io
i=io.StringIO()
remove = ("\000")
remove2 = ("^@")
# name of csv file
filename = "inventory.csv"
# writing to csv file
with open('inventory.csv', 'a', newline='') as csvfile:
# creating a csv dict writer object
#writer = csv.DictWriter(csvfile ,delimiter=',', fieldnames=['ip','name','zone','machinetype','project','created','license','status2'])
fields = ['name','ip','zone','machinetype','project','created','license','status']
writer = csv.DictWriter(csvfile, fieldnames=fields)
# writing headers (field names)
writer.writeheader()
# Assuming 'data.json' contains your JSON data
with open('allinv.json', 'r') as f:
data = json.load(f)
# Accessing data from the loaded JSON
for item in data['_meta']['hostvars']:
with open("servers.txt", "a") as f:
print(item, file=f)
with open('servers.txt') as topo_file:
for line in topo_file:
f = open('inventory.csv', "a")
try:
ip = (line.strip())
#print (ip)
#print (",", end='')
for name in data['_meta']['hostvars'][line.strip()]['name']:
print("".join([name[0] for name in name.split()]), end='', file=i)
instance = i.getvalue()
instance2 = instance.strip()
#print (instance2)
i.truncate(0)
#z = str(name)
#print (",", end='')
for zone in data['_meta']['hostvars'][line.strip()]['zone']:
print (zone, end='', file=i)
zone = i.getvalue()
zone2 = zone.strip()
#print (zone)
i.truncate(0)
#print (",", end='')
for machinetype in data['_meta']['hostvars'][line.strip()]['machineType']:
print (machinetype, end='', file=i)
machinetype = i.getvalue()
machinetype2 = machinetype.strip()
#print (machinetype)
i.truncate(0)
#print (",", end='')
for status in data['_meta']['hostvars'][line.strip()]['status']:
print (status, end='', file=i)
status = i.getvalue()
status2 = status.strip()
#print (status)
i.truncate(0)
#print (",", end='')
for project in data['_meta']['hostvars'][line.strip()]['project']:
print (project, end='', file=i)
project = i.getvalue()
#print (project)
i.truncate(0)
#print (",", end='')
for creation in data['_meta']['hostvars'][line.strip()]['creationTimestamp']:
print (creation, end='',file=i)
creation = i.getvalue()
#print (creation)
i.truncate(0)
#print (",", end='')
for license in data['_meta']['hostvars'][line.strip()]['disks'][0]['licenses']:
print (license, "\n", end='',file=i)
license1 = i.getvalue()
clean_license = license1.replace("\n", " ")
#cleaner_license = clean_license + '\n'
#print (clean_license)
i.truncate(0)
except KeyError as ke:
#print(" None")
clean_license = ("None" )
#x = (instance2,",",ip, ",", zone2, ",", machinetype2, ",", project, ",", creation, ",", clean_license)
print (instance2,",",ip, ",", zone2, ",", machinetype2, ",", project, ",", creation, ",", clean_license, ",", status2, "\n", file=f, end='')#, file=csv
with open (filename, "r") as file:
filedata = file.read()
filedata = filedata.replace(remove, '')
with open(filename, "w") as file:
file.write(filedata)
#with open(filename, 'rb+') as filehandle:
# filehandle.seek(-1, os.SEEK_END)
# filehandle.truncate()
#file = open('inventory.csv','r')
#lines = file.readlines()
#lines = lines[:-1]
##lines.append(lines[-1].strip())
#file.close()
#file = open('inventory.csv','w')
#file.writelines(lines)
#lines = open(filename, "r").readlines()
#lastline = (lines[-1].rstrip()- "\n"
#open(filename, 'w').writelines(lines)
with open(filename, "r")as r, open(
'inventory2.csv', 'w') as o:
data = r.read()
data = data.replace('\000', '')
o.write(data)
#with open(
# "inventory.csv", 'r') as r, open(
# 'inventory2.csv', 'w') as o:
# for line in r:
#strip() function
# if line.strip():
# o.write(line)
#f = open("output.txt", "r")
#print("New text file:\n",f.read())