aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2025-06-09 09:49:42 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2025-06-09 09:49:42 +0200
commitd534518418f465c908211d25623628e8673818c3 (patch)
tree850ecd5db22cfaa23ab8e0afb2a8b1a1625d3f0a
parentb67989737a1aa1972eafdafec5f36eb2a37939f9 (diff)
downloadinfra-d534518418f465c908211d25623628e8673818c3.tar.gz
infra-d534518418f465c908211d25623628e8673818c3.tar.bz2
infra-d534518418f465c908211d25623628e8673818c3.tar.xz
infra-d534518418f465c908211d25623628e8673818c3.zip
netbox
-rw-r--r--ansible/netbox/sync-unifi.py59
1 files changed, 45 insertions, 14 deletions
diff --git a/ansible/netbox/sync-unifi.py b/ansible/netbox/sync-unifi.py
index 4ee9c84..1df5e35 100644
--- a/ansible/netbox/sync-unifi.py
+++ b/ansible/netbox/sync-unifi.py
@@ -52,7 +52,7 @@ class NetboxCache():
self.device_types[slug] = dt
return dt
- def get_or_create_ip_address(self, addr: str, vrf: Record | None):
+ def get_or_create_ip_address(self, addr: str, vrf: Record | None, data):
vrf_id = vrf.id if vrf is not None else None
key = (addr, vrf_id)
ip = self.ip_addresses.get(key)
@@ -61,7 +61,9 @@ class NetboxCache():
ip = self.nb.ipam.ip_addresses.get(address=addr, vrf_id=vrf_id)
if ip is not None:
- print(f"Found IP address {ip.id}")
+ print(f"Found IP address {ip.id} address={ip.address}, vrf={ip.vrf}")
+ ip.update(data)
+ ip = self.nb.ipam.ip_addresses.get(address=addr, vrf_id=vrf_id)
self.ip_addresses[key] = ip
return ip
@@ -69,27 +71,56 @@ class NetboxCache():
self.ip_addresses[key] = ip
return ip
-def process_switch(d: UnifiDevice, db: Db, nb: NetboxCache, site, vrf):
- ip = nb.get_or_create_ip_address(f"{d.ip}/32", vrf)
+ def create_or_update_device(self, d):
+ device = self.nb.dcim.devices.get(name = d["name"])
+ if device is None:
+ device = self.nb.dcim.devices.create(d)
+ print(f"Created device id={device.id}, name={device.name}")
+
+ return device
+
+ print(f"Updating device id={device.id}, name={device.name}")
+ device.update(d)
+
+ return device
+
+ def create_or_update_interface(self, i):
+ iface = self.nb.dcim.interfaces.get(device_id=i["device"], name=i["name"])
+ if iface is None:
+ iface = self.nb.dcim.interfaces.create(i)
+ print(f"Created interface id={iface.id}, name={iface.name}")
- d = {
+ return iface
+
+ print(f"Updating interface id={iface.id}, name={iface.name}")
+ iface.update(i)
+
+ return iface
+
+def process_switch(d: UnifiDevice, db: Db, nb: NetboxCache, site, vrf):
+ device = nb.create_or_update_device({
"name": d.name,
"device_type": nb.get_device_type("ubiquiti-us-8-150w").id,
"role": nb.get_device_role("switch").id,
-# "mac": d.mac,
"serial": d.serial,
"site": site.id,
+ })
+
+ i = {
+ "device": device.id,
+ "name": "switch0",
+# "mac": d.mac, TODO: create the ipam.mac first
+ "type": "virtual",
}
+ iface = nb.create_or_update_interface(i)
- device = nb.nb.dcim.devices.get(name = d["name"])
- if device is None:
- device = nb.nb.dcim.devices.create(d)
- print(f"Created device id={device.id}, name={device.name}")
- else:
- print(f"Updating device id={device.id}, name={device.name}")
- device.update(d)
+ ip = nb.get_or_create_ip_address(f"{d.ip}/32", vrf, {
+ "assigned_object": iface,
+ "assigned_object_id": iface.id,
+ "assigned_object_type": "dcim.interface",
+ })
-# pprint(device.serialize())
+ pprint(ip.serialize())
def main():
unifi_url=os.getenv("UNIFI_URL")