diff --git a/netbox_agent/config.py b/netbox_agent/config.py index cc58a127..68d7d826 100644 --- a/netbox_agent/config.py +++ b/netbox_agent/config.py @@ -28,6 +28,8 @@ def get_config(): p.add_argument('--update-inventory', action='store_true', help='Update inventory') p.add_argument('--update-location', action='store_true', help='Update location') p.add_argument('--update-psu', action='store_true', help='Update PSU') + p.add_argument('--purge-old-devices', action='store_true', + help='Purge existing (old ?) devices having same name but different serial') p.add_argument('--expansion-as-device', action='store_true', help='Manage blade expansions as external devices') diff --git a/netbox_agent/server.py b/netbox_agent/server.py index 0613b8fa..8b9480da 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -242,6 +242,13 @@ def _netbox_create_blade_expansion(self, chassis, datacenter, tenant, rack): ) return new_blade + def _netbox_deduplicate_server(self): + serial = self.get_service_tag() + hostname = self.get_hostname() + server = nb.dcim.devices.get(name=hostname) + if server and server.serial != serial: + server.delete() + def _netbox_create_server(self, datacenter, tenant, rack): device_role = get_device_role(config.device.server_role) device_type = get_device_type(self.get_product_name()) @@ -351,6 +358,9 @@ def netbox_create_or_update(self, config): rack = self.get_netbox_rack() tenant = self.get_netbox_tenant() + if config.purge_old_devices: + self._netbox_deduplicate_server() + if self.is_blade(): chassis = nb.dcim.devices.get( serial=self.get_chassis_service_tag()