From 8d63279730d9268ed65ef5bf140b9faf259c0602 Mon Sep 17 00:00:00 2001 From: Bryan Joshua Pedini Date: Fri, 20 Nov 2026 20:37:47 +0100 Subject: [PATCH] mikrotik-netbox-dns-sync deployment --- .gitignore | 3 +++ README.md | 27 +++++++++++++++++++++++++++ crontab | 2 ++ docker-compose.yml | 26 ++++++++++++++++++++++++++ env.example | 19 +++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 .gitignore create mode 100644 crontab create mode 100644 docker-compose.yml create mode 100644 env.example diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30ed369 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +data +docker-compose.override.yml diff --git a/README.md b/README.md index ca98f45..5bb74ff 100644 --- a/README.md +++ b/README.md @@ -1 +1,28 @@ # mikrotik-netbox-dns-sync Deployment +NetBox to MikroTik static DNS sync: [source/mikrotik-netbox-dns-sync](https://git.bjphoster.com/source/mikrotik-netbox-dns-sync). + +Runs the script every 5 minutes on the [cron image](https://git.bjphoster.com/docker/cron) (`bryanpedini/cron`), logging to the container output. Nothing is built: the crontab and a checkout of the source repo are bind mounted, and `python3` (plus `tzdata`) is installed with `apk` when the container starts. + +## Usage +```sh +cp env.example .env +chmod 600 .env +$EDITOR .env +git clone https://git.bjphoster.com/source/mikrotik-netbox-dns-sync.git data/source +docker compose up -d +``` +`crontab` must be owned by `0:0`, as crond requires. + +Every `SYNC_*` variable in `.env` is passed to the script without the prefix (`SYNC_NETBOX_URL` becomes `NETBOX_URL`, and so on), except `SYNC_SOURCE`, the path of the source checkout. See the source README for every variable and for the NetBox and RouterOS permissions. + +`env.example` ships with `SYNC_DRY_RUN=true`. Run one sync by hand and check the plan before setting it to `false` and running `docker compose up -d` again: +```sh +docker compose exec cron python3 /jobs/netbox_dns_sync.py +docker compose logs -f +``` + +## Update +```sh +git -C data/source pull +``` +The next run picks up the new script, no restart needed. Crontab changes need a `docker compose restart`. diff --git a/crontab b/crontab new file mode 100644 index 0000000..cdbf1c9 --- /dev/null +++ b/crontab @@ -0,0 +1,2 @@ +# min hour day month weekday command +*/5 * * * * /usr/bin/python3 /jobs/netbox_dns_sync.py > /proc/1/fd/1 2>&1 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..22680f5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +--- +services: + cron: + image: bryanpedini/cron:${CRON_VERSION} + restart: unless-stopped + entrypoint: ["sh", "-c", "apk add --no-cache python3 tzdata && exec /docker-entrypoint.sh"] + environment: + - TZ=${TZ} + - NETBOX_URL=${SYNC_NETBOX_URL} + - NETBOX_TOKEN=${SYNC_NETBOX_TOKEN} + - NETBOX_VERIFY_SSL=${SYNC_NETBOX_VERIFY_SSL} + - SUBNET=${SYNC_SUBNET} + - MIKROTIK_HOST=${SYNC_MIKROTIK_HOST} + - MIKROTIK_PORT=${SYNC_MIKROTIK_PORT} + - MIKROTIK_USER=${SYNC_MIKROTIK_USER} + - MIKROTIK_PASSWORD=${SYNC_MIKROTIK_PASSWORD} + - MIKROTIK_VERIFY_SSL=${SYNC_MIKROTIK_VERIFY_SSL} + - DNS_TTL=${SYNC_DNS_TTL} + - MANAGE_ALL_STATIC=${SYNC_MANAGE_ALL_STATIC} + - DRY_RUN=${SYNC_DRY_RUN} + - TIMEOUT=${SYNC_TIMEOUT} + - LOG_FILE=${SYNC_LOG_FILE} + - LOG_TO_CONSOLE=${SYNC_LOG_TO_CONSOLE} + volumes: + - ./crontab:/crontab/root:ro + - ${SYNC_SOURCE}:/jobs:ro diff --git a/env.example b/env.example new file mode 100644 index 0000000..74064f0 --- /dev/null +++ b/env.example @@ -0,0 +1,19 @@ +CRON_VERSION=1.0-alpine3.16 +TZ=Europe/Rome + +SYNC_SOURCE=./data/source +SYNC_NETBOX_URL=https://netbox.example.com +SYNC_NETBOX_TOKEN=nbt_changeme.changeme +SYNC_NETBOX_VERIFY_SSL=true +SYNC_SUBNET=192.0.2.0/24 +SYNC_MIKROTIK_HOST=router.example.com +SYNC_MIKROTIK_PORT=8729 +SYNC_MIKROTIK_USER=netbox-dns-sync +SYNC_MIKROTIK_PASSWORD=changeme +SYNC_MIKROTIK_VERIFY_SSL=false +SYNC_DNS_TTL=5m +SYNC_MANAGE_ALL_STATIC=false +SYNC_DRY_RUN=true +SYNC_TIMEOUT=15 +SYNC_LOG_FILE= +SYNC_LOG_TO_CONSOLE=true