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
|
#!/usr/bin/env python3
import os
import sys
import argparse
import pathlib
import requests
import json
import urllib.parse
def call_katello_api(Outfile=None):
try:
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
resp = requests.get('https://katello.example.com/api/subnets', verify=False, auth=(args.user, args.passwd))
if not resp.ok:
if resp.status_code >= 400:
print(resp.text)
return False
except requests.exceptions.RequestException as e:
print(e)
sys.exit(1)
subnets = resp.json()['results'] # the 'results' key is a list (Python list) of subnets
for subnet in subnets:
# multiple subnets can have same VLAN, e.g. there's 3740 for both Data Network (QTS) and Data Network (ELK)
if subnet['vlanid'] == int(args.vlanid):
try:
# curl -s -k -u "user:pass" -X GET 'https://katello.example.com/api/v2/hosts?search=subnet.name%3D%22Data+Network+%28QTS%29%22'
url = '%s/%s?%s' % ('https://katello.example.com/api/v2', 'hosts', 'search=subnet.name' + urllib.parse.quote_plus('="' + subnet['name'] + '"') + '&per_page=500')
print('\n%s, calling API endpoint: %s' % (subnet['name'], url))
resp = requests.get(url, verify=False, auth=(args.user, args.passwd))
if not resp.ok:
return False
hosts = resp.json()['results']
for host in hosts:
# vlan,ip,hostname,location,os,vm,desc,dept
record = "%s,%s,%s,%s,%s,%s,%s,%s\n" % (args.vlanid,host['ip'],host['name'],host['location_name'],host['operatingsystem_name'],host['model_name'],host['hostgroup_name'].capitalize() if host['hostgroup_name'] is not None else 'N/A','GasOps')
print(record)
if Outfile is not None:
Outfile.write(record)
except requests.exceptions.RequestException as e:
print(e)
sys.exit(1)
return True
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Gets inventory of hosts using Katello API based on given VLAN ID and writes it to a CSV file", parents=())
parser.add_argument("-u", "--user", dest='user', help="katello user", required=True)
parser.add_argument("-p", "--passwd", dest='passwd', help="katello password", required=True)
parser.add_argument("-v", "--vlan", dest='vlanid', help="vlan id of the subnet", required=True)
parser.add_argument("-o", "--output", dest='path', default="stdout", help="optional path where to write a CSV file containing inventory of hosts")
args = parser.parse_args()
outfile = None
if args.path != "stdout":
outpath = pathlib.Path(args.path).resolve()
outfile = open(os.path.join(outpath, 'vlan-' + args.vlanid + '-host-inventory.csv'), 'w')
outfile.write('VLAN,IP,Network Name,Location,Operating System,Virtual or Physical,Description,Department\n')
success = call_katello_api(Outfile=outfile)
if not success:
print("did not get successfull http response")
if outfile:
outfile.close()
# some notes
#
# Katello API doc: https://theforeman.org/plugins/katello/3.17/api/apidoc/v2.html
# list all hosts in Data Network (QTS) subnet in Katello: https://katello.example.com/hosts?search=subnet.name%3D%22Data+Network+%28QTS%29%22
|