From c90a78e1605486243220671869ad1cda5851b181 Mon Sep 17 00:00:00 2001 From: karpenok Date: Sun, 15 Nov 2026 11:59:40 +0700 Subject: [PATCH] =?UTF-8?q?fix(netbird):=20=D0=BE=D1=82=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B8=D1=82=D1=8C=20IPv6/DNS-=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D1=85=D0=B2=D0=B0=D1=82=20=D0=BD=D0=B0=20Exit=20Node=20=D0=B8?= =?UTF-8?q?=20=D1=81=D1=83=D0=B7=D0=B8=D1=82=D1=8C=20=D0=BC=D0=B0=D1=80?= =?UTF-8?q?=D1=88=D1=80=D1=83=D1=82=20LAN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roles/ger-nod-02/netbird/tasks/main.yml | 31 ++++++++++++++++--- .../rus-central/netbird/files/access.yml | 6 ++-- .../rus-central/netbird/tasks/bootstrap.yml | 29 +++++++++++------ 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/ansible/roles/ger-nod-02/netbird/tasks/main.yml b/ansible/roles/ger-nod-02/netbird/tasks/main.yml index e235360..04b1ec1 100644 --- a/ansible/roles/ger-nod-02/netbird/tasks/main.yml +++ b/ansible/roles/ger-nod-02/netbird/tasks/main.yml @@ -5,6 +5,12 @@ content: | net.ipv4.ip_forward = 1 net.ipv4.conf.all.src_valid_mark = 1 + net.ipv4.conf.all.rp_filter = 2 + net.ipv4.conf.default.rp_filter = 2 + net.ipv6.conf.all.disable_ipv6 = 1 + net.ipv6.conf.default.disable_ipv6 = 1 + net.core.default_qdisc = fq + net.ipv4.tcp_congestion_control = bbr owner: root group: root mode: "0644" @@ -24,6 +30,12 @@ ansible.builtin.include_role: name: netbird_client +- name: Перезапустить NetBird после изменения sysctl + ansible.builtin.systemd: + name: netbird + state: restarted + when: netbird_exit_sysctl is changed + - name: Установить jq для синхронизации Exit Node через API NetBird ansible.builtin.apt: name: jq @@ -84,8 +96,13 @@ exit_clients_group_id=$(printf '%s' "${groups_json}" | jq -r --arg g "${NB_EXIT_CLIENTS_GROUP}" '.[] | select(.name == $g) | .id' | head -n 1) if [ -n "${exit_clients_group_id}" ]; then routes_json=$(curl -fsS --max-time 20 -H "Authorization: Token ${NB_API_TOKEN}" "${API_BASE}/routes") - existing_exit_route_id=$(printf '%s' "${routes_json}" | jq -r --arg nid "${NB_EXIT_NET_ID}" '.[] | select(.network_id == $nid and .network == "0.0.0.0/0") | .id' | head -n 1) - if [ -z "${existing_exit_route_id}" ]; then + existing_exit_route=$(printf '%s' "${routes_json}" | jq -c --arg nid "${NB_EXIT_NET_ID}" '.[] | select(.network_id == $nid and .network == "0.0.0.0/0")' | head -n 1) + existing_exit_route_id=$(printf '%s' "${existing_exit_route}" | jq -r '.id // empty') + needs_exit_route_update="false" + if [ -n "${existing_exit_route_id}" ]; then + needs_exit_route_update=$(printf '%s' "${existing_exit_route}" | jq -r '(.metric != 9999) or (.keep_route != false)') + fi + if [ -z "${existing_exit_route_id}" ] || [ "${needs_exit_route_update}" = "true" ]; then exit_route_payload=$(jq -n \ --arg desc "Exit Node DE (0.0.0.0/0 via ${NB_PEER_HOST})" \ --arg nid "${NB_EXIT_NET_ID}" \ @@ -97,13 +114,17 @@ enabled: true, peer_groups: [$eg], network: "0.0.0.0/0", - metric: 100, + metric: 9999, masquerade: true, groups: [$cg], - keep_route: true, + keep_route: false, skip_auto_apply: false }') - nb_api_call POST "/routes" "${exit_route_payload}" >/dev/null + if [ -z "${existing_exit_route_id}" ]; then + nb_api_call POST "/routes" "${exit_route_payload}" >/dev/null + else + nb_api_call PUT "/routes/${existing_exit_route_id}" "${exit_route_payload}" >/dev/null + fi exit_route_created=1 fi fi diff --git a/ansible/roles/rus-central/netbird/files/access.yml b/ansible/roles/rus-central/netbird/files/access.yml index f21762c..a0ca63b 100644 --- a/ansible/roles/rus-central/netbird/files/access.yml +++ b/ansible/roles/rus-central/netbird/files/access.yml @@ -16,9 +16,9 @@ exit_nodes: peer_group: exit-ger-nod-02 client_groups: - exit-clients - metric: 100 + metric: 9999 masquerade: true - keep_route: true + keep_route: false skip_auto_apply: false enabled: true @@ -32,7 +32,7 @@ dns_nameservers: groups: - exit-clients primary: true - enabled: true + enabled: false # Ключи подключения клиентов (секреты сохраняются в Infisical /services/netbird). # Чтобы выпустить отдельный ключ — добавьте запись с новым name и secret. diff --git a/ansible/roles/rus-central/netbird/tasks/bootstrap.yml b/ansible/roles/rus-central/netbird/tasks/bootstrap.yml index 6fdf9a4..d9625ad 100644 --- a/ansible/roles/rus-central/netbird/tasks/bootstrap.yml +++ b/ansible/roles/rus-central/netbird/tasks/bootstrap.yml @@ -117,7 +117,6 @@ fi groups_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/groups") - all_group_id=$(printf '%s' "${groups_json}" | jq -r '.[] | select(.name == "All") | .id' | head -n 1) router_group_id=$(printf '%s' "${groups_json}" | jq -r --arg g "$NB_ROUTER_GROUP" '.[] | select(.name == $g) | .id' | head -n 1) if [ -z "${router_group_id}" ]; then @@ -144,15 +143,20 @@ fi routes_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/routes") - existing_route_id=$(printf '%s' "${routes_json}" | jq -r --arg nid "$NB_LAN_NET_ID" --arg net "$NB_LAN_SUBNET" '.[] | select(.network_id == $nid or .network == $net) | .id' | head -n 1) + existing_route=$(printf '%s' "${routes_json}" | jq -c --arg nid "$NB_LAN_NET_ID" --arg net "$NB_LAN_SUBNET" '.[] | select(.network_id == $nid or .network == $net)' | head -n 1) + existing_route_id=$(printf '%s' "${existing_route}" | jq -r '.id // empty' 2>/dev/null || true) + needs_lan_route_update="false" + if [ -n "${existing_route_id}" ]; then + needs_lan_route_update=$(printf '%s' "${existing_route}" | jq -r --arg rmg "$remna_group_id" '((.groups // []) | sort) != ([$rmg] | sort)') + fi - if [ -z "${existing_route_id}" ]; then + if [ -z "${existing_route_id}" ] || [ "${needs_lan_route_update}" = "true" ]; then route_payload=$(jq -n \ --arg desc "Homelab LAN (${NB_LAN_SUBNET} via OPNsense)" \ --arg nid "$NB_LAN_NET_ID" \ --arg net "$NB_LAN_SUBNET" \ --arg rg "$router_group_id" \ - --arg ag "$all_group_id" \ + --arg rmg "$remna_group_id" \ '{ description: $desc, network_id: $nid, @@ -161,13 +165,20 @@ network: $net, metric: 100, masquerade: true, - groups: [$ag], + groups: [$rmg], keep_route: true }') - curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/routes" \ - -H "Authorization: Token ${active_token}" \ - -H "Content-Type: application/json" \ - -d "${route_payload}" >/dev/null + if [ -z "${existing_route_id}" ]; then + curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/routes" \ + -H "Authorization: Token ${active_token}" \ + -H "Content-Type: application/json" \ + -d "${route_payload}" >/dev/null + else + curl "${CURL_OPTS[@]}" -X PUT "${API_BASE}/routes/${existing_route_id}" \ + -H "Authorization: Token ${active_token}" \ + -H "Content-Type: application/json" \ + -d "${route_payload}" >/dev/null + fi route_created=1 fi