Pynetbox call isn't creating devices, but can return all devices

843 views
Skip to first unread message

cmc

unread,
Apr 25, 2018, 11:58:05 AM4/25/18
to NetBox
Having a bit of hassle creating virtual machines using pynetbox. I can connect to it and return devices through this script

import pynetbox

vm_name='vm1'

nb = pynetbox.api('http://my-netbox-instance.com', token='my-token')

print(nb.virtualization.virtual_machines.all())

nb.virtualization.virtual_machines.create(name='TestVM',cluster='Generic')

quit()


This returns 

['vm1', 'vm2', 'vm3']

But is doesn't create the virtual machine 'TestVM'. Am I missing something here with my syntax?

zmo...@digitalocean.com

unread,
Apr 26, 2018, 11:57:48 AM4/26/18
to NetBox
You'll need to reference cluster by its id instead of name. You should also get back a RequestError exception that you can catch and use to get more details about the error. Feel free to open up an item on pynetbox's issue tracker for stuff like this since it doesn't have its own mailing list.

cmc

unread,
Apr 27, 2018, 4:59:03 AM4/27/18
to NetBox
Thanks! That's why I wasn't seeing any return then. Is there anywhere in the documentation I can look at for assigning a new IP/interfaces to a new VM?

cmc

unread,
Apr 27, 2018, 10:02:55 AM4/27/18
to NetBox
A simpler one even, I'm trying to create a new VM with 
import pynetbox
from config import *

vm_name='vmtest'

nb = pynetbox.api(NETBOX_URL, token=NETBOX_TOKEN)

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

It creates the vm, but doesn't give it an IP address and doesn't throw any error. Am I missing syntax somewhere here?

Brian Candler

unread,
Apr 29, 2018, 7:42:25 AM4/29/18
to NetBox
I'm not an API user but I believe it's a three-step process:

1. Create the VM
2. Create an interface and associate it to the VM (using the VM id)
3. Create an IP address and associate it with the interface (using the interface id)

cmc

unread,
Apr 29, 2018, 10:45:23 AM4/29/18
to NetBox
Got those steps up and running, but can't assign an IP to the VM without the IP ID. Is there a reason 
nb.ipam.ip_addresses(address='111.111.111.111/32').id

doesn't return the IP ID?

Brian Candler

unread,
Apr 30, 2018, 3:10:17 AM4/30/18
to NetBox
Don't you get the id as part of the return value when creating the IP?

cmc

unread,
Apr 30, 2018, 3:43:33 AM4/30/18
to NetBox
Yeah just figured out you can get it as part of the device dictionary with ['id'] when creating it
Reply all
Reply to author
Forward
0 new messages