Closed shepherdjay closed 1 year ago
It's easily done e.g. in Python:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pynetbox
nb = pynetbox.api("https://your.netbox.server.here", token="your_token_that_can_change_assets")
asset = nb.plugins.inventory.assets.get(id=1234)
device = nb.dcim.devices.get(id=9876)
asset.device = device
asset.save()
Of course you can use any other criteria than ID to get the asset and the device like "name".
If I'm not mistaken you can also just use either the numerical ID to assign the device (asset.device = 9876
) or the device's name (asset.device = 'my_device_name'
). But I'm not 100% sure, you need to check for yourself.
@cs-1 -- This was exactly what I needed and addresses my concerns I had about whether there would be an issue with doing the save even though the UI API Explorer was a bit more confusing to find. This is what I ended up coming up with and it worked well.
devices = list(nb.dcim.devices.all())
assets = list(nb.plugins.inventory.assets.all())
device_sn_lookup = {device.serial: device for device in devices}
for asset in assets:
if asset.device is None:
try:
asset.device = device_sn_lookup[asset.serial]
asset.save()
except KeyError:
print(f"{asset.serial} could not be located in dcim")
Apologies -- I couldn't find if this was covered or not and did some of the exploration of the codebase and api but plugin development in netbox is still a little new to me.
Is there an API method of assigning an asset to a device? I'm able to pull a list of assets and I see in the API for each asset there is a
device
dictionary that appears that includes the deviceid
,url
,display
, andname
but it isn't clear if this is safe to just update this or if there is a better endpoint for doing a programmatic assignment.Got myself into this situation because of doing a bulk import where we bulk imported the devices separately from the assets and would like to avoid manually trying to reassign.