167 lines
6.9 KiB
YAML
167 lines
6.9 KiB
YAML
---
|
|
- 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
|
|
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"
|
|
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: Перезапустить NetBird после изменения sysctl
|
|
ansible.builtin.systemd:
|
|
name: netbird
|
|
state: restarted
|
|
when: netbird_exit_sysctl is changed
|
|
|
|
- name: Дождаться переподключения NetBird после перезапуска
|
|
ansible.builtin.command: netbird status
|
|
register: netbird_status_after_restart
|
|
changed_when: false
|
|
retries: 20
|
|
delay: 3
|
|
until: "'Management: Connected' in netbird_status_after_restart.stdout"
|
|
when: netbird_exit_sysctl is changed
|
|
|
|
- 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=$(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}" \
|
|
--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: 9999,
|
|
masquerade: true,
|
|
groups: [$cg],
|
|
keep_route: false,
|
|
skip_auto_apply: false
|
|
}')
|
|
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
|
|
|
|
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
|