Assigning prefixes to vlans

338 views
Skip to first unread message

Timothy Summerton

unread,
Jan 19, 2021, 6:06:28 AM1/19/21
to NetBox
Hi

I'm very new to netbox and I'm having an issue bulk importing and assigning prefixes to vlans.

I've attempted to google fixes but so far had no joy.

Here is an extract from my import script:

prefix,site,role,status,vlan
172.20.1.0/26,Techno Centre,WEB-Proxy,Active,3
172.20.1.0/25,Techno Centre,WEB-Proxy,Active,3

this is the info for a manually created prefix on the vlan 3:
 "prefix": "172.20.1.128/27",
            "site": {
                "id": 2,
                "url": "http://localhost:8000/api/dcim/sites/2/",
                "name": "Techno Centre",
                "slug": "techno-center"
            },
            "vrf": null,
            "tenant": null,
            "vlan": {
                "id": 1299,
                "url": "http://localhost:8000/api/ipam/vlans/1299/",
                "vid": 3,
                "name": "x1_service_3",
                "display_name": "x1_service_3 (3)"
            
This is the response after selecting the "submit" button:

Enter the list of column headers followed by one line per record to be imported, using commas to separate values. Multi-line data and values containing commas may be wrapped in double quotes.
  • Row 1 vlan: Object not found.
I've even attempted creating it via an api request:

curl -X PUT -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" -H "Content-Type: application/json" -H "Accept: application/json; indent=4" "http://localhost:8000/api/ipam/prefixes/767/" -d "{ \"prefix\": \"172.20.1.0/26\", \"site\": 2, \"vrf\": 0, \"tenant\": 0, \"vlan\":3, \"status\": \"container\", \"role\": 0, \"is_pool\": true, \"description\": \"string\", \"tags\": [ { \"name\": \"string\", \"slug\": \"string\", \"color\": \"string\" } ], \"custom_fields\": {}}"
{
    "detail": "Not found."

Any assistance will be much appreciated.

thank you

Brian Candler

unread,
Jan 19, 2021, 9:01:37 AM1/19/21
to NetBox
Try using the vlan name "x1_service_3" instead of "3" for the "vlan" column.

The vid (802.1q tag) is not a unique identifier. That is, Netbox lets you have multiple VLANs with tag 3, typically in different sites.

Timothy Summerton

unread,
Jan 19, 2021, 1:20:14 PM1/19/21
to NetBox
thanks Brain, 

After a full day of testing i found a work-a-round

Locate the TRUE vlan id

curl -X GET -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" -H "Content-Type: application/json" -H "Accept: application/json; indent=4" "http://localhost:8000/api/ipam/vlans/?vid=6"
{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1302,
            "url": "http://localhost:8000/api/ipam/vlans/1302/",
            "site": {
                "id": 2,
                "url": "http://localhost:8000/api/dcim/sites/2/",
                "name": "TechnoCentre",
                "slug": "techno-center"
            },
            "group": {
                "id": 11,
                "url": "http://localhost:8000/api/ipam/vlan-groups/11/",
                "name": "Service_Network",
                "slug": "service_network"
            },
            "vid": 6,
            "name": "x1_service_6",
            "tenant": null,
            "status": {
                "value": "active",
                "label": "Active"
            },
            "role": {
                "id": 1,
                "url": "http://localhost:8000/api/ipam/roles/1/",
                "name": "WEB-Proxy",
                "slug": "WEB-Proxy"
            },
            "description": "",
            "tags": [],
            "display_name": "x1_service_6 (6)",
            "custom_fields": {},
            "created": "2021-01-19",
            "last_updated": "2021-01-19T06:18:14.683866Z",
            "prefix_count": null
        }
    ]
}    

Now you can assign the prefix to the vlan with the following api call:

curl -X POST -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" -H "Content-Type: application/json" -H "Accept: application/json; indent=4" "http://localhost:8000/api/ipam/prefixes/" -d "{ \"prefix\": \"172.20.249.128/25\", \"site\": 2, \"vlan\": 1302, \"status\": \"active\", \"role\": 1, \"is_pool\": true, \"tags\": [ ], \"custom_fields\": {}}"
{
    "id": 4469,
    "family": {
        "value": 4,
        "label": "IPv4"
    },
    "prefix": " 172.20.249.128/25 ",
    "site": {
        "id": 2,
        "name": "TechnoCentre",
        "slug": "techno-center"
    },
    "vrf": null,
    "tenant": null,
    "vlan": {
        "id": 1795,
        "url": "http://localhost:8000/api/ipam/vlans/ 1302 /",
        "vid": 499,
        "name": "6",
        "display_name": "x1_service_6 (6)"
    },
    "status": {
        "value": "active",
        "label": "Active"
    },
    "role": {
        "id": 1,
        "name": "WEB-Proxy",
        "slug": "WEB-Proxy"
    },
    "is_pool": true,
    "description": "",
    "tags": [],
    "custom_fields": {},
    "created": "2021-01-19",
    "last_updated": "2021-01-19T18:14:03.795228Z"
}                                         

Truly hope this can help someone else:-)

Cheers

Brian Candler

unread,
Jan 20, 2021, 3:30:45 AM1/20/21
to NetBox
You said you were using CSV, not API, to create the prefixes.

If you're using the API, then there may be simpler solutions.  You can specify the vlan using a selection criterion that results in a unique vlan.

e.g. instead of
...{"vlan": 1302}, ....

you should be able to put something like this (untested):
...{"vlan": {"vid": 6, "site": 2}}, ...

Reply all
Reply to author
Forward
0 new messages