scsd-configs/code/central_devices.py
2025-03-05 15:22:17 -05:00

65 lines
1.6 KiB
Python

import centralauth
import requests
import json
DEBUG = False
centralauth_db=centralauth.get_centralauth()
all_text="""
all:
vars:
ansible_network_os: arubanetworks.aoscx.aoscx
ansible_connection: arubanetworks.aoscx.aoscx # REST API via pyaoscx connection method
ansible_aoscx_validate_certs: False
ansible_aoscx_use_proxy: False
ansible_acx_no_proxy: True
ansible_ssh_common_args: "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
"""
headers = {
'Accept': 'application/json',
'Authorization': f'Bearer {centralauth_db['access_token']}',
}
offset=1
page_size=300
switch_dict={}
while True:
url = f"{centralauth_db['base_url']}/monitoring/v1/switches?limit={page_size}&offset={offset}"
response = requests.get(url, headers=headers)
#print(response.text)
j=json.loads(response.text)
if j['count']==0:
break
for switch in j['switches']:
if not switch['status'] == 'Down' and not switch['ip_address']=='':
switch_dict[switch['name']]=switch
#print (switch['name'],switch['serial'],switch['ip_address'])
offset+=page_size
current_building=''
for switch_name in sorted(switch_dict):
switch=switch_dict[switch_name]
building=switch_name.split("-")[0]
if not building == current_building:
print(f"{building}:")
print( " hosts:")
current_building=building
print(f" {switch_name}:")
print(f" ansible_host: {switch['ip_address']}")
print(f" building_name: {current_building}")
print(f" serial_num: {switch['serial']}")
print(f"{all_text}")