feat(netbird): настроить ger-nod-02 как Exit Node для клиентов
ansible-ger-nod-02 / deploy (push) Successful in 21s
ansible-rus-central / deploy (push) Successful in 26s

This commit is contained in:
karpenok committed 2026-11-14 12:36:07 +07:00
1 parent 2eadd3aedc
commit 3178455672
4 files changed
+303 -4

No files matched your search

@@ -1,4 +1,136 @@
---
- name: Зафиксировать параметры ядра для NetBird Exit Node
ansible.builtin.copy:
dest: /etc/sysctl.d/99-netbird-exit.conf
content: |
net.ipv4.ip_forward = 1
net.ipv4.conf.all.src_valid_mark = 1
owner: root
group: root
mode: "0644"
register: netbird_exit_sysctl
- name: Применить параметры sysctl для Exit Node
ansible.builtin.command: sysctl --system
when: netbird_exit_sysctl is changed
changed_when: true
- name: Разрешить порт WireGuard NetBird (51820/udp) для прямого P2P и Exit Node
ansible.builtin.command: ufw allow 51820/udp
register: ufw_netbird_wg
changed_when: "'added' in (ufw_netbird_wg.stdout | lower)"
- name: Клиент NetBird на ноде
ansible.builtin.include_role:
name: netbird_client
- name: Установить jq для синхронизации Exit Node через API NetBird
ansible.builtin.apt:
name: jq
state: present
- name: Привязать ger-nod-02 к группе Exit Node и проверить маршрут 0.0.0.0/0
ansible.builtin.shell: |
set -euo pipefail
API_BASE="https://central.gunihome.ru/api"
nb_api_call() {
local method="$1" path="$2" body="$3"
local resp code
resp=$(curl -sS --max-time 20 -w '\n%{http_code}' -X "$method" "${API_BASE}${path}" \
-H "Authorization: Token ${NB_API_TOKEN}" \
-H "Content-Type: application/json" \
-d "${body}")
code=${resp##*$'\n'}
resp=${resp%$'\n'*}
if [ "${code}" != "200" ] && [ "${code}" != "201" ]; then
echo "NetBird API ${method} ${path} failed (HTTP ${code}): ${resp}" >&2
return 1
fi
printf '%s' "${resp}"
}
exit_group_updated=0
exit_route_created=0
peers_json=$(curl -fsS --max-time 20 -H "Authorization: Token ${NB_API_TOKEN}" "${API_BASE}/peers")
ger_peer_id=$(printf '%s' "${peers_json}" | jq -r --arg h "${NB_PEER_HOST}" '.[] | select((.name // "") == $h or (.hostname // "") == $h) | .id' | head -n 1)
if [ -z "${ger_peer_id}" ]; then
echo "Пир ${NB_PEER_HOST} не найден в NetBird API" >&2
exit 1
fi
groups_json=$(curl -fsS --max-time 20 -H "Authorization: Token ${NB_API_TOKEN}" "${API_BASE}/groups")
exit_group_id=$(printf '%s' "${groups_json}" | jq -r --arg g "${NB_EXIT_GROUP}" '.[] | select(.name == $g) | .id' | head -n 1)
if [ -z "${exit_group_id}" ]; then
grp_payload=$(jq -n --arg name "${NB_EXIT_GROUP}" --arg pid "${ger_peer_id}" '{name: $name, peers: [$pid]}')
grp_resp=$(nb_api_call POST "/groups" "${grp_payload}")
exit_group_id=$(printf '%s' "${grp_resp}" | jq -r '.id // empty')
if [ -z "${exit_group_id}" ]; then
echo "Не удалось создать группу ${NB_EXIT_GROUP}" >&2
exit 1
fi
exit_group_updated=1
else
exit_grp_obj=$(printf '%s' "${groups_json}" | jq -c --arg id "${exit_group_id}" '.[] | select(.id == $id)')
has_ger=$(printf '%s' "${exit_grp_obj}" | jq -r --arg pid "${ger_peer_id}" '[.peers[]?.id] | index($pid) != null')
if [ "${has_ger}" != "true" ]; then
upd_payload=$(printf '%s' "${exit_grp_obj}" | jq -c --arg pid "${ger_peer_id}" '{name: .name, peers: (([.peers[]?.id] + [$pid]) | unique)}')
nb_api_call PUT "/groups/${exit_group_id}" "${upd_payload}" >/dev/null
exit_group_updated=1
fi
fi
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
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}" \
--arg eg "${exit_group_id}" \
--arg cg "${exit_clients_group_id}" \
'{
description: $desc,
network_id: $nid,
enabled: true,
peer_groups: [$eg],
network: "0.0.0.0/0",
metric: 100,
masquerade: true,
groups: [$cg],
keep_route: true,
skip_auto_apply: false
}')
nb_api_call POST "/routes" "${exit_route_payload}" >/dev/null
exit_route_created=1
fi
fi
jq -n \
--argjson exit_group_updated "${exit_group_updated}" \
--argjson exit_route_created "${exit_route_created}" \
'{
exit_group_updated: ($exit_group_updated == 1),
exit_route_created: ($exit_route_created == 1),
changed: (($exit_group_updated + $exit_route_created) > 0)
}'
args:
executable: /bin/bash
environment:
NB_API_TOKEN: "{{ lookup('env', 'NETBIRD_API_TOKEN', default='') }}"
NB_PEER_HOST: "{{ inventory_hostname }}"
NB_EXIT_GROUP: "exit-ger-nod-02"
NB_EXIT_CLIENTS_GROUP: "exit-clients"
NB_EXIT_NET_ID: "exit-ger-nod-02"
when: lookup('env', 'NETBIRD_API_TOKEN', default='') | length > 0
register: ger_netbird_exit_sync
changed_when: (ger_netbird_exit_sync.stdout | from_json).changed
no_log: true
- name: Дождаться применения конфигурации Exit Node на клиенте NetBird
ansible.builtin.command: netbird status
register: ger_netbird_status_after
changed_when: false
when: ger_netbird_exit_sync is changed