Skip to content

Instantly share code, notes, and snippets.

@XioNoX
Created October 17, 2018 16:12
Show Gist options
  • Save XioNoX/cad54f613c1a6d655eb424e9bd79b1bf to your computer and use it in GitHub Desktop.
Save XioNoX/cad54f613c1a6d655eb424e9bd79b1bf to your computer and use it in GitHub Desktop.
Script to mass add PSUs to specific servers/sites in Netbox
#!/usr/bin/env python
import pynetbox
netbox_api_token = 'secret'
print('Connecting to Netbox')
netbox = pynetbox.api('https://netbox', token=netbox_api_token)
# Pull list of eqsin dell devices from netbox
devices = netbox.dcim.devices.filter(manufacturer='dell', site='eqsin')
# itterate over them
for device in devices:
device_id = device.id
# If it doesn't have an existing PSU, create them
if not netbox.dcim.power_ports.filter(device_id=device_id):
print('Create PSUs on ' + device.name)
netbox.dcim.power_ports.create({'name': 'PSU1', 'device': device_id})
netbox.dcim.power_ports.create({'name': 'PSU2', 'device': device_id})
else:
# Otherwise ignore the device
print('PSU already exists on ' + device.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment