Skip to content

Recipes

Outcome: the everyday jobs, done the NetBox way. Each recipe assumes the install from chapter 3, a token from chapter 9, and the lab's API shapes. The first two are assembled entirely from steps the lab ran; the rest follow the same verified shapes.

Record a new device, end to end

The full chain from chapters 5 to 8, in order, spending each new ID in the next call:

  1. Check the site exists (GET /api/dcim/sites/?slug=...); create it if not.
  2. Manufacturer and device type: reuse if the model is already known, create if not.
  3. Create the device with its type, role, site and status (chapter 6's JSON is the template).
  4. Create its interfaces; assign an address from the site prefix (chapter 7); PATCH the device's primary address.
  5. Record its cables last, once both ends exist (chapter 8).

netbox-lab/api-proof.sh is this recipe with the lab's fixture names, and its transcript is the proof the sequence works in one pass.

Find a free address in a prefix

Read the prefix's record and its addresses, and let the accounting work: GET /api/ipam/prefixes/ shows each block, and the prefix's own page (and its available_ips in the API's vocabulary) shows the space not yet handed out. The discipline is the recipe: never take an address from memory or from pinging — take it from the record, then create the address record before configuring the device, so the map leads and the network follows. (The lab verified the prefix and address records this reads from; the address hunt over a large, lived-in prefix is the same reads at scale.)

Answer "what is this plugged into?"

Read the interface (GET /api/dcim/interfaces/<id>/): its record names its cable and the cable's other end. For the lab's cable that is one hop — lab-sw-01 eth0 to lab-sw-02 eth0 — read back in t11-cable.txt. Across patch panels the same read walks the recorded path.

Retire a device without lying

Set the device's status to decommissioning while it is still physically present and reachable, and to offline (or delete the record, deliberately) only when it is gone. Delete its address assignments as the addresses return to the pool, so the prefix's free space becomes true again. The map's value is that its statements match the world; retiring in the open keeps it so.

Give a script its own token

One token per script, never a person's token copied into a file: create it under a service account, read-only unless the script must write, with an expiry date, and note what it is for in its description. When the script is retired, delete the token and the key to the map room stops working — the discipline chapter 9's token design exists to support. (Token creation through the shell is the lab's verified route; the web pages offer the same object under the user's profile menu.)

Back up the truth

NetBox the application is replaceable; the PostgreSQL database is the truth. A backup recipe is therefore a database recipe: dump the netbox database with PostgreSQL's pg_dump, keep configuration.py beside the dump (it holds the secret key and the token pepper, without which sessions and v2 tokens cannot be validated), and test the restore on a scratch install before trusting it. The dump tool is PostgreSQL's, covered by its own guide in this series; the NetBox-specific part is the pairing of dump and configuration, stated here from the model and not run against the lab install.