Netbox - populating - Python
by JJJCR from LinuxQuestions.org on (#6FPQM)
Hi guys,
Anyone who can give an idea or an example on how to populate netbox?
Able to create a VM, with resources like Vcpus, Memory, Disk Space. And it's showing on the GUI.
Able to add interface.
But assigning IP Address to Interface is a bit of a challenge.
IP Address is shown on Netbox IPAM but it doesn't show on the interface object nor on the VM overview dashboard.
Using Pynetbox library, any one can give an example or a guide on how to do it?
======
Code:import pynetbox
# Initialize the NetBox API connection
nb = pynetbox.api(
url='http://http://127.0.0.1:8000,
token='xxx-api-token'
)
def create_ip_address(address, **kwargs):
ip_data = {
"address": address,
**kwargs
}
try:
return nb.ipam.ip_addresses.create(ip_data)
except Exception as e:
print(f"Failed to create IP address: {e}")
return None
vm_name = "VM_Name"
interface_name = "eth0"
vm = nb.virtualization.virtual_machines.get(name=vm_name)
if not vm:
print(f"Virtual machine {vm_name} not found!")
exit()
interface = nb.virtualization.interfaces.get(virtual_machine_id=vm.id, name=interface_name)
if not interface:
interface_data = {
"name": interface_name,
"type": "virtual",
"virtual_machine": vm.id
}
interface = nb.virtualization.interfaces.create(interface_data)
if not interface:
print(f"Failed to create interface {interface_name} for VM {vm_name}!")
exit()
# Assign the IP address to the interface using the create_ip_address function
new_ip = create_ip_address("192.168.22.10/24", status="active", description="Test IP", interface=interface.id)
if new_ip:
print(f"Successfully assigned IP address {new_ip.address} to interface {interface_name} on VM {vm_name}.")
else:
print("Failed to assign IP address.")Got this code copied from the internet it seems to work fine to create vm and vm resource values, interface also created
but the IP Address does not seem to work
its not even displayed on the GUI
ipam seems to be ok
any ideas? thanks
Anyone who can give an idea or an example on how to populate netbox?
Able to create a VM, with resources like Vcpus, Memory, Disk Space. And it's showing on the GUI.
Able to add interface.
But assigning IP Address to Interface is a bit of a challenge.
IP Address is shown on Netbox IPAM but it doesn't show on the interface object nor on the VM overview dashboard.
Using Pynetbox library, any one can give an example or a guide on how to do it?
======
Code:import pynetbox
# Initialize the NetBox API connection
nb = pynetbox.api(
url='http://http://127.0.0.1:8000,
token='xxx-api-token'
)
def create_ip_address(address, **kwargs):
ip_data = {
"address": address,
**kwargs
}
try:
return nb.ipam.ip_addresses.create(ip_data)
except Exception as e:
print(f"Failed to create IP address: {e}")
return None
vm_name = "VM_Name"
interface_name = "eth0"
vm = nb.virtualization.virtual_machines.get(name=vm_name)
if not vm:
print(f"Virtual machine {vm_name} not found!")
exit()
interface = nb.virtualization.interfaces.get(virtual_machine_id=vm.id, name=interface_name)
if not interface:
interface_data = {
"name": interface_name,
"type": "virtual",
"virtual_machine": vm.id
}
interface = nb.virtualization.interfaces.create(interface_data)
if not interface:
print(f"Failed to create interface {interface_name} for VM {vm_name}!")
exit()
# Assign the IP address to the interface using the create_ip_address function
new_ip = create_ip_address("192.168.22.10/24", status="active", description="Test IP", interface=interface.id)
if new_ip:
print(f"Successfully assigned IP address {new_ip.address} to interface {interface_name} on VM {vm_name}.")
else:
print("Failed to assign IP address.")Got this code copied from the internet it seems to work fine to create vm and vm resource values, interface also created
but the IP Address does not seem to work
its not even displayed on the GUI
ipam seems to be ok
any ideas? thanks