Pynetbox assign ip to interface.

3,141 views
Skip to first unread message

Георгий Сафронов

unread,
Sep 10, 2021, 12:34:23 PM9/10/21
to NetBox
How i can assign ip to device (or vm) interface, i try:
address = nb.ipam.ip_addresses.get(address="1.1.1.1")
address.assigned_object = {"name": "eth2", "device": {"name": "dev-01"}}
address.save()

and
address.device_id = 200
address.interface_id = 1992
address.save()

it always return True, but did not save :(

Brian Candler

unread,
Sep 10, 2021, 12:59:55 PM9/10/21
to NetBox
pynetbox is a front-end to the REST API, and if you go to /api/docs/ on a running Netbox instance you'll get the API docs.  Browse down to POST /ipam/ip-addresses/ or PUT  ​/ipam​/ip-addresses​/{id}​/.  There you will find:

assigned_object_type: string
assigned_object_id: integer

So I believe those are the two attributes you'd have to set.  I've not tested updating the interface assignment for an existing object, but using a GET query I can see that the valid values for assigned_object_type are "dcim.interface" or "virtualization.vminterface".

HTH,

Brian.

Георгий Сафронов

unread,
Sep 13, 2021, 3:21:55 AM9/13/21
to NetBox
Thank you, you gave me an idea:

address = nb.ipam.ip_addresses.get(address="1.1.1.1")
address.assigned_object_id = 1994
address.assigned_object_type = "dcim.interface"
address.save()
True

That works!
пятница, 10 сентября 2021 г. в 19:59:55 UTC+3, Brian Candler:
Reply all
Reply to author
Forward
0 new messages