ansible-deploy / plan (push) Successful in 2s
ansible-lint / lint (push) Successful in 3s
ansible-deploy / deploy (core-net, ) (push) Failing after 16s
ansible-deploy / deploy (ger-nod-02, ) (push) Successful in 47s
ansible-deploy / deploy (rus-central, ) (push) Successful in 6m20s
200 lines
8.7 KiB
YAML
200 lines
8.7 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 = 0
|
||
net.ipv6.conf.default.disable_ipv6 = 0
|
||
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) только по IPv4
|
||
ansible.builtin.command: "{{ item }}"
|
||
loop:
|
||
- ufw delete allow 51820/udp
|
||
- ufw allow proto udp from any to 0.0.0.0/0 port 51820
|
||
- ufw deny in proto udp from ::/0 to ::/0 port 51820
|
||
- ufw deny out proto udp from ::/0 to ::/0 port 51820
|
||
register: ufw_netbird_wg
|
||
changed_when: "'added' in (ufw_netbird_wg.stdout | lower) or 'deleted' in (ufw_netbird_wg.stdout | lower)"
|
||
failed_when: false
|
||
|
||
- name: Разрешить каскад Xray по IPv6 только с rus-central (1443/tcp)
|
||
ansible.builtin.command: ufw allow from 2a06:dd00:1:4::6bd1 to any port 1443 proto tcp
|
||
register: ufw_cascade_v6
|
||
changed_when: "'added' in (ufw_cascade_v6.stdout | lower)"
|
||
|
||
- name: Запретить любой исходящий доступ с exit node в домашнюю LAN (defense-in-depth)
|
||
ansible.builtin.command: "{{ item }}"
|
||
loop:
|
||
- ufw delete allow out proto tcp from any to 192.168.1.108 port 8428
|
||
- ufw deny out to 192.168.1.0/24
|
||
register: ufw_lan_restrict
|
||
changed_when: "'added' in (ufw_lan_restrict.stdout | lower) or 'deleted' in (ufw_lan_restrict.stdout | lower)"
|
||
failed_when: false
|
||
|
||
- 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
|
||
|
||
# Удалить внешнюю ноду из группы remnawave (панель), чтобы у неё не было прав панели
|
||
remna_grp_obj=$(printf '%s' "${groups_json}" | jq -c '.[] | select(.name == "remnawave")' | head -n 1)
|
||
if [ -n "${remna_grp_obj}" ]; then
|
||
remna_grp_id=$(printf '%s' "${remna_grp_obj}" | jq -r '.id')
|
||
in_remna=$(printf '%s' "${remna_grp_obj}" | jq -r --arg pid "${ger_peer_id}" '[.peers[]?.id] | index($pid) != null')
|
||
if [ "${in_remna}" = "true" ]; then
|
||
remna_upd=$(printf '%s' "${remna_grp_obj}" | jq -c --arg pid "${ger_peer_id}" '{name: .name, peers: ([.peers[]?.id | select(. != $pid)] | unique)}')
|
||
nb_api_call PUT "/groups/${remna_grp_id}" "${remna_upd}" >/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 "${NB_EXIT_DESC}" \
|
||
--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: "{{ netbird_exit_group }}"
|
||
NB_EXIT_CLIENTS_GROUP: "{{ netbird_exit_clients_group }}"
|
||
NB_EXIT_NET_ID: "{{ netbird_exit_network_id }}"
|
||
NB_EXIT_DESC: "{{ netbird_exit_route_description }}"
|
||
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
|