#!/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