A comprehensive NetBox custom script for synchronizing device components with device type templates. This script enables efficient bulk management of interfaces, ports, and bays across your infrastructure.
- Features
- Requirements
- Installation
- Usage
- Operation Modes
- Component Types
- Stack-Aware Interface Renaming
- Configuration Guide
- Testing
- Troubleshooting
- Contributing
- License
- Interfaces - Network interfaces (Ethernet, GigabitEthernet, etc.)
- Rear Ports - Rear panel connection ports
- Front Ports - Front panel connection ports
- Power Ports - Device power input connections
- Power Outlets - Power distribution outlets
- Console Ports - Console management ports
- Console Server Ports - Console server connections
- Module Bays - Expansion module/card slots
- Device Bays - Chassis expansion bays
- Safe Mode - Creates new + Updates existing + Preserves components not in template (least destructive)
- Standard Mode - Creates new + Updates existing + Deletes components not in template
- Clean Sync Mode - Full synchronization with explicit deletion warnings
- Replicate Only - Only creates new components (no updates or deletes)
- Update Only - Only updates existing components (no creates or deletes)
- Stack-Aware Interface Renaming - Automatically rename interfaces in Virtual Chassis environments
- Comprehensive Reporting - Detailed change summary with per-device statistics
- Component Tagging - Auto-tag created components with 'netbox-script' for tracking
- Deletion Warnings - Impact assessment for components being deleted
- Version Compatibility - Graceful handling of different NetBox versions
- Per-Device Statistics - Detailed tracking of changes per device
- NetBox 4.0 or later (Tested on NetBox 4.4.10 ✅)
- Django 3.2 or later
- Python 3.8 or later
- Tagging Support - Either
django-taggingordjango-taggit(optional but recommended)
| Component | Min NetBox Version | Status |
|---|---|---|
| Interfaces, Ports | 1.0+ | ✅ Tested |
| Power Components | 2.0+ | ✅ Tested |
| Console Components | 2.0+ | ✅ Tested |
| Module Bays | 4.0+ | ✅ Tested on 4.4.10 |
| Device Bays | 4.0+ | ✅ Tested on 4.4.10 |
✅ Verified Working on: NetBox 4.4.10
Note: The script includes fallback handling for older versions. Module Bay and Device Bay synchronization will gracefully skip if not available in your NetBox version.
No need to verify NetBox, Django, or Python - Just upload!
- Navigate to Scripts: Go to Customization > Scripts in NetBox
- Click "Add" button to create a new script
- Choose the script file: Click "Choose file" and select
Sync Device Components.py - Create: Click "Create" button
- Done! Script is now ready to use
That's it! NetBox handles everything automatically.
Copy Sync Device Components.py to your NetBox custom scripts directory:
# Linux/macOS
cp "Sync Device Components.py" /opt/netbox/netbox/scripts/
# Windows
Copy "Sync Device Components.py" "C:\netbox\netbox\scripts\"Then restart NetBox:
# Using Docker
docker-compose restart
# Using systemd
sudo systemctl restart netbox netbox-rq
# Using supervisor
sudo supervisorctl restart netbox netbox-rq- Default path:
/opt/netbox/netbox/scripts/ - Check your installation: Settings > System Configuration > Scripts Directory
# Using Docker
docker-compose restart
# Using systemd
sudo systemctl restart netbox netbox-rq
# Using supervisor
sudo supervisorctl restart netbox netbox-rqFor UI Method:
- Log in to NetBox admin panel
- Navigate to Customization > Scripts
- Search for "Sync Device Components from Template"
- Script should appear with green status (✓)
For CLI Method:
- Log in to NetBox admin panel
- Navigate to Customization > Scripts
- Search for "Sync Device Components from Template"
- Script should appear with green status (✓)
- If not visible, restart NetBox services again
- Navigate to Scripts: Go to Customization > Scripts in NetBox UI
- Click the Script: Select "Sync Device Components from Template"
- Configure Parameters: See Configuration Guide
- Preview Changes: Leave "Commit" unchecked first (Preview mode)
- Execute: Click "Execute" button
- Review Report: Examine the generated report for accuracy
- If Satisfied: Run again with "Commit" checked to save changes
- Validate: Review report showing all changes applied
- Validate: Review the execution report and verify changes in NetBox
Operation Mode: Standard Mode
Device Type: Cisco Nexus 9396PX
Component Selection:
✓ Sync Interfaces
☐ Other components
Commit: Unchecked (Preview first)
Operation Mode: Replicate Only
Devices: Select Virtual Chassis devices
Component Selection:
✓ Sync Interfaces
Stack-Aware Rename: Checked
Stack Pattern: (\D+)(\d+)(/.*) $
Commit: Unchecked (Preview first)
Operation Mode: Clean Sync Mode
Devices: Select multiple devices
Component Selection:
✓ Sync Interfaces
✓ Sync Power Ports
✓ Sync Console Ports
Commit: Checked
- ✅ Creates new components from template
- ✅ Updates existing components
- ✅ Preserves components not in template
- Use Case: First-time sync, preserving custom components
- Risk Level: Lowest
- ✅ Creates new components from template
- ✅ Updates existing components
- ❌ Deletes components not in template
- Use Case: Regular maintenance sync
- Risk Level: Medium
- ✅ Creates new components from template
- ✅ Updates existing components
- ❌ Deletes components not in template
⚠️ Displays detailed warnings about deletions- Use Case: Full cleanup with awareness
- Risk Level: High (requires confirmation)
- ✅ Creates new components from template
- ❌ No updates to existing components
- ❌ No deletions
- Use Case: Adding new components only
- Risk Level: Low
- ❌ No new component creation
- ✅ Updates existing components
- ❌ No deletions
- Use Case: Modifying descriptions without adding/removing
- Risk Level: Low
Each component type supports:
- Creation - Add new components matching template
- Update - Modify descriptions and properties
- Deletion - Remove components not in template (in applicable modes)
- Tagging - Automatic tagging for audit trail
- Tracking - Per-device statistics
Type: Network Interface
Examples: Ethernet0/0, GigabitEthernet1/0/1, 100GE1/0/1
Properties:
- Name (required)
- Type (ETH, GE, 100GE, etc.)
- MTU (optional)
- Description (optional)
Rear/Front Ports:
- Name (required)
- Type (RJ45, LC, ST, etc.)
- Positions (optional)
Power Ports/Outlets:
- Name (required)
- Power Type (IEC 60320-C13, NEMA 5-15P, etc.)
- Feed Leg (optional)
Console Ports/Server Ports:
- Name (required)
- Type (RJ45, Mini-DIN 8, etc.)
Module Bays:
- Name (required)
- Position (optional)
- Description (optional)
Device Bays:
- Name (required)
- Position (optional)
- Description (optional)
In Virtual Chassis (VC) environments, multiple devices operate as a single logical unit. Stack-aware renaming automatically adjusts interface names based on each device's position in the stack.
Virtual Chassis Setup:
- Device 1 (Position 1): Member 1
- Device 2 (Position 2): Member 2
- Device 3 (Position 3): Member 3
Device Type Template:
Interfaces:
- GigabitEthernet1/0/1
- GigabitEthernet1/0/2
- GigabitEthernet1/0/3
(etc.)
After Stack-Aware Rename:
| Device Position | Original | Renamed To |
|---|---|---|
| 1 | GigabitEthernet1/0/1 | GigabitEthernet1/0/1 (unchanged) |
| 2 | GigabitEthernet1/0/1 | GigabitEthernet2/0/1 |
| 3 | GigabitEthernet1/0/1 | GigabitEthernet3/0/1 |
The script uses regex patterns to identify the stack position in interface names and replace it.
(\D+)(\d+)(/.*) $Groups:
- Group 1:
(\D+)- Interface prefix (non-digits) e.g.,GigabitEthernet - Group 2:
(\d+)- Stack position digit (gets replaced) e.g.,1 - Group 3:
(/.*)- Port path (remains unchanged) e.g.,/0/1
| Vendor | Pattern | Example |
|---|---|---|
| Cisco IOS | (\D+)(\d+)(/.*)$ |
GigabitEthernet1/0/1 → GigabitEthernet2/0/1 |
| Cisco 100GE | (\D+)(\d+)(/.*)$ |
HundredGigE1/0/1 → HundredGigE2/0/1 |
| Juniper | (\w+)-(\d+)-(\d+) |
ge-1-0-0 → ge-2-0-0 |
| Arista | (Ethernet)(\d+)(/.*)$ |
Ethernet1/1 → Ethernet2/1 |
| HP/Aruba | (Eth)(\d+)(/.*)$ |
Eth1/1 → Eth2/1 |
| Hyphenated | (\w+)-(\d+)$ |
eth-1 → eth-2 |
- Go to regex101.com
- Enter your pattern in the regular expression field
- Paste your interface name in the test string field
- Verify that group 2 captures only the position digit
| Issue | Cause | Solution |
|---|---|---|
| Pattern doesn't match | Wrong anchors or groups | Verify 3 groups, use $ at end |
| All digits captured | Group 1 too greedy | Use \D+ instead of \w+ |
| Position not replaced | Wrong group number | Ensure position is in group 2 |
| Extra characters included | Group 3 incomplete | Include complete remainder in group 3 |
- Required: Yes
- Description: Filter devices by device type (optional for specific type, required if filtering)
- Use: Select the device type whose components you want to sync
- Required: Yes
- Description: Select specific devices to update
- Supports: Multiple selection (Ctrl+Click or Shift+Click)
- Tip: Filter by device type first to narrow selection
- Required: Yes
- Default: Standard Mode
- Options: Safe, Standard, Clean Sync, Replicate Only, Update Only
- Recommendation: Use Safe Mode for first-time sync
- Required: No (at least one must be selected)
- Default: Interfaces only
- Options: Interfaces, Rear Ports, Front Ports, Power Ports, Power Outlets, Console Ports, Console Server Ports, Module Bays, Device Bays
- Note: Select only components you want to sync
- Type: Boolean (Checkbox)
- Default: Unchecked (disabled)
- Required: Regex pattern if enabled
- Use: Virtual Chassis environments
- Prerequisite: Devices must have Virtual Chassis relationships configured
- Type: Text (Regular Expression)
- Default:
(\D+)(\d+)(/.*) $ - Required if: Stack-aware renaming is enabled
- Format: Must contain 3 capture groups
- Example:
(\D+)(\d+)(/.*)$
- Type: Boolean (Checkbox)
- Default: Unchecked (preview mode)
- Important: Always preview changes first (leave unchecked)
- Checked: Saves all changes to database
- Recommendation: Review report before checking
See TESTING.md for comprehensive testing procedures including:
- Pre-deployment validation
- Functional testing in development
- Performance testing
- Rollback procedures
- Production deployment checklist
Problem: Script doesn't show in Customization > Scripts
Solutions:
- Verify file placement:
/opt/netbox/netbox/scripts/ - Check file permissions:
chmod 644 "Sync Device Components.py" - Verify Python syntax:
python -m py_compile "Sync Device Components.py" - Restart NetBox:
docker-compose restartorsystemctl restart netbox - Check NetBox logs for import errors:
docker-compose logs netbox
Problem: "ModuleBay not found" or similar import error
Solution: Your NetBox version may not support this component type
- Module Bays require NetBox 4.0+
- Device Bays require NetBox 4.0+
- Script gracefully handles missing components via try/except
- Check NetBox version: Settings > About
Problem: Interface names not being renamed with stack pattern
Solutions:
- Verify stack pattern is correct regex
- Test pattern on regex101.com
- Ensure devices are part of Virtual Chassis
- Check device VC positions are set correctly
- Verify pattern has 3 capture groups
Problem: Script reports more changes than expected
Solutions:
- Use Preview mode first (uncheck Commit)
- Review report carefully
- Check if device templates have many components
- Consider using "Replicate Only" for test
- Run on fewer devices to verify behavior
Problem: Script runs slowly or times out
Solutions:
- Reduce number of selected devices
- Reduce component types selected
- Check NetBox database performance
- Consider running on subset first
- Check NetBox task timeout settings (default 300s)
The script generates a comprehensive CSV report including:
- Operation mode used
- Device type and selection
- Selected components and options
- Execution timestamp
- Global statistics for each component type
- Total created, updated, deleted counts
- Per-device breakdown table
- Device name
- Created/updated/deleted counts for each component type
- Quick overview of changes per device
- List of all deleted components (if any)
- Timestamp of deletion
- Impact assessment:
- Cables disconnected
- Descriptions removed
- IP addresses removed
- Custom attributes lost
- Preview Mode: Review changes before committing
- Commit Flag: Must explicitly enable to make changes
- Dry-Run Support: Always run with Preview first
- Deletion Warnings: Clear notifications about destructive operations
- Component Tagging: Track script-created components with 'netbox-script' tag
- Audit Trail: Detailed reports of all changes
- Rollback Info: Deleted component details for manual restoration
-
Always Preview First
- Uncheck "Commit" for first run
- Review the report carefully
- Verify changes before committing
-
Start Small
- Test on single device first
- Verify results match expectations
- Expand to more devices
-
Use Safe Mode
- First-time sync: use Safe Mode
- Prevents accidental deletions
- Preserves custom components
-
Document Changes
- Save execution reports
- Track which devices were updated
- Keep audit trail for compliance
-
Test Stack Patterns
- Use regex101.com to verify pattern
- Test on single device first
- Verify renamed interfaces are correct
-
Backup First
- Backup NetBox database before large changes
- Export device configuration first
- Have rollback plan ready
To contribute improvements:
- Test thoroughly in development environment
- Document any new features
- Maintain backward compatibility
- Update this README with changes
- Include test cases
MIT License - Feel free to use and modify for your needs
For issues or questions:
- Check TESTING.md for testing procedures
- Review Troubleshooting section
- Check NetBox logs:
docker-compose logs netbox | grep Sync - Verify NetBox version compatibility
- Test with Preview mode first
Last Updated: January 2026
Version: 1.2
Status: Production Ready ✅