Skip to content

Import from Windows

An import reads the whole configuration of one Windows server, compares it with what NetBox holds for that server, and brings the differences into NetBox for review. It never writes to Windows.

Starting an import

  • Import now on the Windows server page (with netbox-branching, the Import menu, see below), or POST /api/plugins/windows-dhcp/windows-servers/<id>/import/ (see REST API). Runs as the user who asked.
  • On a schedule, see below.

Only one import per server runs at a time, and a new one is not queued while another is queued or running. An import and a push of the same server never run at the same time: an import started by hand that finds a push running fails (run it again later), a scheduled one is queued again (see below), and a push that finds an import running retries (see Workers and jobs).

What is compared

Everything the plugin syncs: classes, option definitions, scopes, superscopes, exclusion ranges, reservations, policies, option values, failover relationships, MAC filters and the list switches, for IPv4, and scopes, reservations, user classes and option values for DHCPv6 (see DHCPv6). Each difference is a create, update or delete of one object, in dependency order (a scope before its reservations).

  • Create: on Windows, not in NetBox.
  • Update: on both sides, with different values.
  • Delete: in NetBox for this server, not on Windows. This includes objects created in NetBox that no push has written yet (sync disabled, or a failed push): dismiss those.

Some Windows data is left out and noted in the sync run instead: DHCPv6 vendor classes and their values, DHCPv6 exclusion ranges, and DHCPv6 values of codes that are not standard DHCPv6 options. A failover relationship is imported from one partner only: the first one imported becomes its primary (the server the plugin writes to), and the other partner's imports leave it alone. Import the server you want to write to first.

Choosing where an import writes

flowchart TD
    start(["Import now, Import menu,<br/>REST action or schedule"]) --> job["Import job in the worker<br/>(one job per server at a time)"]
    job --> read["Read the whole Windows server"]
    read --> diff{"Differences<br/>with NetBox?"}
    diff -- "none" --> done(["Nothing written<br/>sync run says so"])
    diff -- "some" --> branching{"netbox-branching<br/>installed?"}
    branching -- "no" --> proposal["Import proposal<br/>one item per difference"]
    proposal --> apply["Reviewer applies or<br/>dismisses items"]
    apply --> managed(["Applied objects are in NetBox<br/>and recorded as managed, no push"])
    branching -- "yes" --> dest{"Destination"}
    dest -- "Import into branch<br/>(default, schedules)" --> ibranch["Open import branch of the server,<br/>or a new one + change request"]
    ibranch --> merge["Review, approve, merge"]
    merge --> push(["Merge queues a push that finds<br/>nothing to do and adopts the objects"])
    dest -- "Import into this branch" --> mine["The branch you work in<br/>written with your permissions"]
    mine --> merge
    dest -- "Import directly" --> main(["Main, after a confirm page<br/>recorded as managed, no push"])

Without netbox-branching an import always writes an import proposal. With it, the Import button on the Windows server page is a menu:

Choice Writes into Review
Import into branch (the default) the server's open import branch, or a new one the branch (and its change request) is reviewed and merged; main is not changed by the import
Import into this branch (only while you work in a branch) the branch you work in; no new branch, no change request the branch, like your other edits in it
Import directly main none: the changes are in main once the job has run

Import directly first opens a page that names main as the destination and, when you work in a branch, says the import ignores that branch. If an import branch for the server is still open, it says so too: importing into main as well writes the same objects twice, so merging that branch afterwards conflicts. Merge or delete it first, or import into it instead. Nothing is queued until you confirm. It is shown disabled, with the reason, when import_direct is off or when change control's protect_main covers DHCP and IPAM objects and you do not have netbox_change_control.bypass_policy.

Import into this branch names your branch in the request; if you have switched to another branch (or to main) in the meantime, the import is refused rather than sent somewhere else.

The destination goes with the job, so it does not depend on the branch the worker or your browser has active later, and it is checked again when the job starts. A destination that is no longer allowed fails the import with the reason; it never falls back to another one. Scheduled imports always use an import branch.

Imports into main or into your own branch write as you: every object is checked against your permissions as it is saved, and a change you may not make (or that change control refuses) is rolled back and listed in the sync run, while the others stay. Event rules and webhooks only hear about the changes that stayed. Your branch is checked again just before the import writes into it: if it has started merging (or is otherwise no longer ready) by then, nothing is written. An import into main queues no push (NetBox then matches Windows) and records the objects it wrote as managed, as applying a proposal does.

The Import panel and the sync run say where each import went: the branch and its change request, the branch you chose, or main. The Destination column of the sync run list shows it for every import, and can be filtered.

With netbox-branching: import branches

The Import menu on a Windows server with netbox-branching: Import into branch, or Import directly into
main

When the import finds differences, it creates a branch named dhcp-import/<server>/<timestamp> (import_branch_prefix changes the first part), provisions it, and writes the differences into it with change tracking, so the branch's diff shows every change.

  • With netbox-change-control and import_change_request on (the default), it also opens a change request for the branch and submits it for review. Change control approves it only through a policy: see Installation.
  • If an open import branch for the server already exists, the next import writes into it instead of opening another, so it only adds what changed since. A branch is no longer reused once its change request is completed or abandoned, or once it is stale (too far behind main to sync).
  • An import with no differences creates no branch.
  • netbox-branching's max_working_branches applies: when it is reached, the import fails and says so.

The Windows server page has an Import panel. While the import is queued or running it says so, and refreshes itself until the job ends. Once it has finished it names the branch the import wrote into, with links to the branch and its change request, says whether it opened a new branch or added to the open one, and says that main has not changed (until the branch is merged). An import that found nothing new for an existing branch says the branch already holds the differences; one that found nothing at all says no branch was created. After an import that failed or found nothing, an earlier import's branch that is still open stays listed. The sync run page shows the same for its own run. The panel only shows what you may view: sync runs, jobs, branches and change requests.

Once a branch is no longer open, the panel says what became of it instead of calling it open: being merged, merged into main (with the time), or deleted. For a deleted branch it reads the deletion in the change log: merged before it was deleted, deleted without being merged, or, when there is no deletion record you may view, that the outcome is not known. A missing branch is never taken as proof of a merge, and a merged import never means main still matches Windows: import again to compare. The server panel also links the server's import history.

Review the branch like any other: look at its diff, sync it with main if it is behind, then merge it (once the change request is approved). Merging queues a push (when Sync enabled is on), which finds nothing to do: NetBox now matches Windows, and the push records the imported objects as managed. With sync off, the first push after you turn it on does that.

Merging needs no Windows DHCP permission for the import's own changes, even when they move a prefix, range, address or MAC that a scope or reservation uses: they only bring NetBox in line with Windows, and a sync of main's edits to other fields (description, tags), or a hand edit of those fields in the branch, does not change that. A change of the prefix, range, address or MAC that someone adds to the import branch by hand (or that a sync brings in from main) is checked like any other, when it is made and again at merge, and so is every change of that object. Reverting an import branch is always checked: it puts back values Windows no longer holds (see Editing IPAM objects used by DHCP).

Writing into a branch needs a NetBox user: manual imports use the user who clicked, scheduled ones the import_user setting. Starting an import also needs permission to add a branch, and to add a change request when one will be opened (see Permissions).

Once the job has finished, the Import panel on the server page links the branch, its change request and the sync run, and says that main has not changed:

The Import panel after an import into a branch: 78 differences imported into a new branch, main not changed,
the branch, change request CR3 and the sync run

The branch's Diff tab lists every object the import created, changed or deleted. Review it there, then merge it (through the change request, with netbox-change-control):

The Diff tab of the import branch: the objects the import created, from classes to failover relationships and MAC
filters

The change request of an import, approved by a reviewer and ready to merge

Without branching: import proposals

The import writes an import proposal (Windows DHCP > Import proposals) with one item per difference (the Import panel on the server page links to it once the job has finished): the action, the kind and Windows key of the object, its values before and after, and the NetBox object it touches.

On a proposal you can:

  • Apply all or Dismiss all pending items;
  • select items in the list and Apply selected or Dismiss selected;
  • open one item and Apply or Dismiss it.

One proposal item: create reservation 198.18.60.0/198.18.60.30 (handmade01, made by hand on Windows), with what
NetBox holds (nothing yet) and what Windows holds

Proposals and items are written by the import, so they cannot be added or edited by hand; they can be deleted.

Applying writes to NetBox as the reviewing user, so the change log shows who accepted each change. It checks that the NetBox object still has the item's "before" values; if someone changed it since the import, the item fails and says so. It also checks the user's permissions: an item whose main object the user may not write is left pending and the missing permission is reported; every other object it writes (prefixes and addresses included) is checked as it is saved, and a refusal rolls back the whole item and marks it failed. Failed items can be applied again later. Applied items do not trigger a push (NetBox and Windows then agree), and the applied objects are recorded as managed straight away.

Large reviews run in the background. Applying or dismissing more open items at once than the review_job_threshold setting (100 by default) would take too long for one web request: the first import of a large server can propose thousands of changes. Above that number, Apply all, Dismiss all and the bulk buttons queue a Windows DHCP import review job instead and link it in the message. The job runs as you, with the same permission checks (made again when it starts), acts only on the items you picked, and records the changes in the change log under your name with the job's ID as the request ID. The proposal page shows the newest review job in a Background review panel, with its counts once it has finished, and lists all of them on its Jobs tab. Who asked, the error and the link to the job only show to users who may view jobs (core.view_job). One review job per proposal runs at a time: another click while one waits says so (a job that no worker finishes stops blocking after 2 hours). Picked items that a newer import replaced, or that someone else decided, before the job started are skipped and counted; when none is left, the job fails and asks you to review again. A job that stops halfway (a timeout, a crash) keeps what it had already done and says how far it got. The job waits for no import or push of the same server: if one is running, it fails and says so, and you apply again once it has finished.

Proposals are applied to main. If netbox-branching was installed after a proposal was written, its items cannot be applied while you work in a branch: nothing is written and the page says to switch to main (the REST API answers 409). Dismissing still works. When change control's protect_main keeps you from writing to main, dismiss the old proposal and import into a branch instead.

Each item moves through these states:

stateDiagram-v2
    [*] --> pending: import finds a difference
    pending --> applied: Apply (NetBox changed as you)
    pending --> dismissed: Dismiss
    pending --> failed: object changed since the import,<br/>or a save was refused
    failed --> applied: Apply again
    failed --> dismissed: Dismiss
    pending --> [*]: next import replaces it
    failed --> [*]: next import replaces it
    dismissed --> pending: next import sees other<br/>Windows values
    applied --> [*]

A repeated import updates the open proposal: pending and failed items are replaced by the new differences. A dismissed item stays dismissed, in later proposals too, as long as Windows still shows exactly the same values; if the Windows values change, it comes back. An import with nothing new to review opens no proposal. The Import panel and the sync run then say that nothing new waits for review and how many earlier dismissed differences stay ignored, rather than "No differences found", which only appears when NetBox and Windows really agree. When some differences are new and others were dismissed before, the count waiting for review leaves the dismissed ones out. Because a repeated import moves the open proposal to the newest run, an earlier sync run says its differences were sent for review and links the server's open proposal.

Scheduled imports

Set Import interval (minutes) on a Windows server. A NetBox system job runs every minute and queues an import for each server whose last import started at least that long ago, unless one is already queued or running. A scheduled import that finds its server busy (another import or a push running) is queued again a minute later, for up to half an hour, instead of waiting a whole interval; the waiting attempts leave no failed sync run and send no notification. The same system job deletes old sync runs (sync_run_retention_days, 90 days by default, see Workers and jobs).

Scheduled imports run as the user named in import_user. With branching they fail without it: a branch and its change request need an owner. Create a dedicated user, give it the permissions an import needs, and set:

PLUGINS_CONFIG = {
    "netbox_windows_dhcp_plugin": {
        "import_user": "dhcp-import-bot",
    },
}

With branching, each scheduled import that finds differences adds to the open import branch, so a server changed by hand every day builds up one branch to review rather than one per run.

What an import creates in NetBox

  • the server's scopes, superscopes, exclusion ranges, reservations, classes, option definitions, option values, policies, failover relationships, MAC filters and list switches, with their Windows names (see How objects map);
  • the prefixes, IP ranges, IP addresses and MAC addresses the scopes and reservations point at. A new scope's prefix goes in the server's import VRF; a scope's first IP range and its reservations' IP addresses follow the VRF of the scope's prefix, which only differs for a scope made by hand on a prefix elsewhere. Existing ones are reused, so failover partners share one IP range per scope. An import does not move an IP range that a scope of another server uses unless the two are failover partners for it in NetBox: see Limitations.

The scopes after an import: Windows scope name, server, prefix, IP range, state, superscope and
description

An imported scope: name and description from Windows, server, prefix, IP range, superscope, lease duration, and
the Leases tab

The IPv4 settings panel of an imported scope: type, lease duration, offer delay, max BOOTP clients and activate
policies

Text going into a length-limited field is cut to that length on both sides, so a long Windows name does not show as a difference on every run.