how to assign ip addr to an interface on the virtual machine

2,000 views
Skip to first unread message

N R

unread,
Jan 17, 2018, 4:50:32 PM1/17/18
to NetBox
Hi All,

I am using pynetbox python bindings to talk to the nebox api. I am not sure if this is the right platform to ask the question as I don't find any serious and well detailed documentation anywhere else.

1. I am trying to create a virtual machine. 
2. Add an interface to the created VM.
3. Add an IP Address to the interface that we just created.

=================

import pynetbox

vm_name='vm1'

nb = pynetbox.api('http://xx.xx.xx.xx:8000', token='xxxxxxxxxxxxxxxxx')

print nb

vrf_obj=nb.ipam.vrfs.get(name='test')
print "vrf_obj is : %s" %vrf_obj.id

nb.virtualization.virtual_machines.create(name=vm_name,status=1,cluster='1')


vm_obj = nb.virtualization.virtual_machines.get(name=var_name)

if vm_obj != None:
    nb.virtualization.interfaces.create(virtual_machine=vm_obj.id,name='eth1')
else:
    print "vm with the given name doesn't exists in the netbox inventory."


nb.ipam.ip_addresses.create(address='12.11.11.11/24',status=1,vrf_id=vrf_obj.id)

#Assign the IP ADDR to the interface on the vm

nb.ipam.ip_addresses.put(address='11.11.11.11/24',status=1,vrf_id=vrf_obj.id,virtual_machine_id=6,interface_id=25)
#ip_obj=nb.ipam.ip_addresses.get('10.231.6.0/23')
#print ip_obj

quit()

=================

up the second step, I am able to get create the objects in the netbox. However, I can't seem to make it to work for assigning the IP address to the "eth1", I created for "VM1".

Any help is appreciated?

Thanks,
No1.

Brian Candler

unread,
Jan 18, 2018, 4:37:03 PM1/18/18
to NetBox
> However, I can't seem to make it to work for assigning the IP address to the "eth1", I created for "VM1".

Do you get an error? If so what does it say?

Also, you have hard-coded virtual_machine_id=6 and interface_id=25 (instsead of using the values returned by the API).  Are you sure those are correct?

Also, have you tried creating the IP and assigning it to the interface at the same time, when you create the IP?

Olof Lundgren

unread,
Nov 30, 2018, 6:51:16 AM11/30/18
to NetBox
did you solve this? i still havent found out how to  add an ip address to a virtual machines interface. i just get 404. 

my attempt:
nb.ipam.ip_address.create(
address=ip_addr,
status=1,
virtual_machine=o6_test.id,
interface_id=create_interface.get('id'),
)
In [21]: o6_test.id Out[21]: 3 In [22]: create_interface.get('id') Out[22]: 330 In [23]: ip_addr Out[23]: '::1/128'

Brian Candler

unread,
Nov 30, 2018, 10:40:58 AM11/30/18
to NetBox
The virtual machine is an attribute of the interface, not the IP address.  That is, the model goes like this:

Virtual Machine ----< Interface ----< IP Address

When you create the Interface, you assign it to the Virtual Machine (or device).  And then when you create the IP Address, you attach it to the Interface.
Reply all
Reply to author
Forward
0 new messages