refactor(remnawave,exit-node): split bootstrap/sync and generalize ger-nod-02 roles
ansible-core-net / deploy (push) Successful in 1m50s
ansible-core-mon / deploy (push) Successful in 28s
ansible-core-secrets / deploy (push) Successful in 18s
ansible-ger-nod-02 / deploy (push) Failing after 3s
ansible-opnsense / deploy (push) Successful in 9s
ansible-rus-central / deploy (push) Successful in 1m7s

This commit is contained in:
karpenok committed 2026-11-15 22:31:04 +07:00
1 parent 83baec4bad
commit 2fc6e39323
19 files changed
+1092 -863

No files matched your search

@@ -0,0 +1,21 @@
# Декларативная конфигурация профилей, нод и хостов Remnawave.
# Добавление новой выходной VPS: добавить запись в nodes и прогнать ansible-core-net.
xray_template:
name: Except-RU
file: xray-except-ru.json
squad_name: Default-Squad
subscription_page_config: Homelab
nodes:
- name: Ger-nod-02
address: "150.241.64.209"
netbird_hostname: "ger-nod-02"
port: 2222
country_code: "DE"
profile_name: "DE-Yali"
profile_template: "profile-selfsteal-xhttp.json"
inbound_tag: "Steal-Yali"
selfsteal_domain: "yali.gunis.top"
xhttp_path: "/xhttp-yali"
host_remark: "🇩🇪 ger-nod-02"
@@ -13,7 +13,7 @@
},
"inbounds": [
{
"tag": "Steal-Yali",
"tag": "__INBOUND_TAG__",
"port": 443,
"protocol": "vless",
"settings": {
@@ -28,9 +28,9 @@
"network": "xhttp",
"security": "reality",
"xhttpSettings": {
"host": "yali.gunis.top",
"host": "__SELFSTEAL_DOMAIN__",
"mode": "stream-one",
"path": "/xhttp-yali"
"path": "__XHTTP_PATH__"
},
"realitySettings": {
"dest": "/dev/shm/nginx.sock",
@@ -39,7 +39,7 @@
"spiderX": "",
"shortIds": ["__REALITY_SHORT_ID__"],
"privateKey": "__REALITY_PRIVATE_KEY__",
"serverNames": ["yali.gunis.top"]
"serverNames": ["__SELFSTEAL_DOMAIN__"]
}
}
}
@@ -0,0 +1,592 @@
#!/usr/bin/env python3
"""Идемпотентная синхронизация шаблонов, профилей, нод, хостов, сквадов и пользователей Remnawave."""
from __future__ import annotations
import json
import os
import re
import secrets
import subprocess
import sys
from typing import Any
def _redact_text(text: str) -> str:
"""Убирает потенциально чувствительные поля при выводе ошибок."""
return re.sub(
r'("(?:privateKey|privKey|secretKey|accessToken|apiToken|password|token|vlessUuid|trojanPassword|ssPassword|shortUuid)"\s*:\s*)"[^"]*"',
r'\1"[REDACTED]"',
text,
flags=re.IGNORECASE,
)
class RemnawaveAPI:
"""Минимальный клиент Remnawave API через curl с --resolve на 127.0.0.1."""
def __init__(self, domain: str, token: str) -> None:
self.domain = domain
self.base_url = f"https://{domain}/api"
self.token = token
def request(
self,
method: str,
path: str,
payload: dict[str, Any] | None = None,
allow_error: bool = False,
) -> tuple[int, Any]:
cmd = [
"curl",
"-sSk",
"--resolve",
f"{self.domain}:443:127.0.0.1",
"-w",
"\n%{http_code}",
"-X",
method,
f"{self.base_url}{path}",
"-H",
"X-Remnawave-Client-Type: browser",
"-H",
f"Authorization: Bearer {self.token}",
]
stdin_bytes = None
if payload is not None:
cmd.extend(["-H", "Content-Type: application/json", "--data-binary", "@-"])
stdin_bytes = json.dumps(payload, ensure_ascii=False).encode("utf-8")
proc = subprocess.run(cmd, input=stdin_bytes, capture_output=True, check=False)
if proc.returncode != 0:
raise RuntimeError(
f"curl error for {method} {path}: {proc.stderr.decode(errors='replace')}"
)
raw = proc.stdout.decode("utf-8", errors="replace")
body, _, code_str = raw.rpartition("\n")
status = int(code_str.strip() or "0")
data: Any = None
if body.strip():
try:
data = json.loads(body)
except json.JSONDecodeError:
data = body
if not (200 <= status < 300):
if allow_error:
return status, data
raise RuntimeError(
f"Remnawave API {method} {path} failed (HTTP {status}): {_redact_text(body)}"
)
return status, data
def resolve_netbird_peers(peers_url: str, nb_token: str) -> dict[str, str]:
"""Возвращает карту hostname/name/dns_label -> IPv4 из NetBird API."""
if not peers_url or not nb_token:
return {}
cmd = [
"curl",
"-fsS",
"--max-time",
"20",
"-H",
f"Authorization: Token {nb_token}",
peers_url,
]
proc = subprocess.run(cmd, capture_output=True, check=False)
if proc.returncode != 0:
return {}
try:
raw = json.loads(proc.stdout.decode("utf-8", errors="replace"))
except json.JSONDecodeError:
return {}
peers = raw if isinstance(raw, list) else (raw.get("items") or raw.get("peers") or [])
mapping: dict[str, str] = {}
ip_re = re.compile(r"^\d+\.\d+\.\d+\.\d+$")
for peer in peers:
if not isinstance(peer, dict):
continue
ip = str(peer.get("ip") or "").strip()
if not ip_re.match(ip):
continue
for key in ("hostname", "name", "dns_label"):
val = str(peer.get(key) or "").strip()
if val and val not in mapping:
mapping[val] = ip
return mapping
def _norm_iso(val: str | None) -> str:
if not val:
return ""
return val.replace(".000Z", "Z")
def _extract_list(resp: Any, *keys: str) -> list[dict[str, Any]]:
if isinstance(resp, dict):
inner = resp.get("response", resp)
if isinstance(inner, list):
return inner
if isinstance(inner, dict):
for k in keys:
if isinstance(inner.get(k), list):
return inner[k]
if isinstance(resp, list):
return resp
return []
def sync_xray_template(
api: RemnawaveAPI,
tpl_name: str,
tpl_json: dict[str, Any],
changes: list[str],
) -> str:
_, tpl_list = api.request("GET", "/subscription-templates/")
templates = _extract_list(tpl_list, "templates")
tpl_uuid = next(
(
t["uuid"]
for t in templates
if t.get("name") == tpl_name and t.get("templateType") == "XRAY_JSON"
),
"",
)
if not tpl_uuid:
_, created = api.request(
"POST",
"/subscription-templates/",
{"name": tpl_name, "templateType": "XRAY_JSON", "templateJson": tpl_json},
)
tpl_uuid = (created.get("response") or created).get("uuid", "")
if not tpl_uuid:
raise RuntimeError(f"Не удалось создать шаблон {tpl_name}")
changes.append(f"created xray template {tpl_name}")
else:
_, tpl_full = api.request("GET", f"/subscription-templates/{tpl_uuid}")
current = (tpl_full.get("response") or {}).get("templateJson") or {}
if current != tpl_json:
api.request(
"PATCH",
"/subscription-templates/",
{"uuid": tpl_uuid, "templateJson": tpl_json},
)
changes.append(f"updated xray template {tpl_name}")
return tpl_uuid
def ensure_profile_and_inbound(
api: RemnawaveAPI,
node_cfg: dict[str, Any],
profile_templates: dict[str, str],
changes: list[str],
) -> tuple[str, str]:
profile_name = node_cfg["profile_name"]
inbound_tag = node_cfg["inbound_tag"]
_, profiles_resp = api.request("GET", "/config-profiles/")
profiles = _extract_list(profiles_resp, "configProfiles")
profile_uuid = next((p["uuid"] for p in profiles if p.get("name") == profile_name), "")
if not profile_uuid:
tpl_key = node_cfg["profile_template"]
raw_tpl = profile_templates.get(tpl_key)
if not raw_tpl:
raise RuntimeError(f"Не найден шаблон профиля {tpl_key}")
_, key_resp = api.request("GET", "/system/tools/x25519/generate")
resp_obj = key_resp.get("response") or key_resp
keypairs = resp_obj.get("keypairs") or []
priv = (
(keypairs[0].get("privateKey") if keypairs else None)
or resp_obj.get("privateKey")
or resp_obj.get("privKey")
or ""
)
if not priv:
raise RuntimeError("Не удалось сгенерировать X25519 ключ Reality")
short_id = secrets.token_hex(8)
rendered = (
raw_tpl.replace("__REALITY_PRIVATE_KEY__", priv)
.replace("__REALITY_SHORT_ID__", short_id)
.replace("__INBOUND_TAG__", inbound_tag)
.replace("__SELFSTEAL_DOMAIN__", node_cfg["selfsteal_domain"])
.replace("__XHTTP_PATH__", node_cfg["xhttp_path"])
)
_, created = api.request(
"POST",
"/config-profiles/",
{"name": profile_name, "config": json.loads(rendered)},
)
profile_uuid = (created.get("response") or created).get("uuid", "")
if not profile_uuid:
raise RuntimeError(f"Не удалось создать config profile {profile_name}")
changes.append(f"created profile {profile_name}")
_, profile_full = api.request("GET", f"/config-profiles/{profile_uuid}")
inbounds = _extract_list(profile_full, "inbounds")
if not inbounds:
_, inb_resp = api.request("GET", f"/config-profiles/{profile_uuid}/inbounds")
inbounds = _extract_list(inb_resp, "inbounds")
inbound_uuid = next((i["uuid"] for i in inbounds if i.get("tag") == inbound_tag), "")
if not inbound_uuid:
raise RuntimeError(f"Не найден inbound {inbound_tag} в профиле {profile_uuid}")
return profile_uuid, inbound_uuid
def sync_node_and_host(
api: RemnawaveAPI,
node_cfg: dict[str, Any],
profile_uuid: str,
inbound_uuid: str,
tpl_uuid: str,
nb_peers: dict[str, str],
netbird_connected: bool,
changes: list[str],
warnings: list[str],
) -> None:
node_name = node_cfg["name"]
target_addr = node_cfg["address"]
nb_host = node_cfg.get("netbird_hostname", "")
if netbird_connected and nb_host and nb_host in nb_peers:
target_addr = nb_peers[nb_host]
elif nb_host:
warnings.append(
f"node {node_name}: используется fallback-адрес {target_addr} (NetBird peer {nb_host} недоступен)"
)
_, nodes_resp = api.request("GET", "/nodes/")
nodes = _extract_list(nodes_resp)
existing_node = next((n for n in nodes if n.get("name") == node_name), None)
desired_cp = {
"activeConfigProfileUuid": profile_uuid,
"activeInbounds": [inbound_uuid],
}
if not existing_node:
_, created = api.request(
"POST",
"/nodes/",
{
"name": node_name,
"address": target_addr,
"port": int(node_cfg["port"]),
"countryCode": node_cfg["country_code"],
"configProfile": desired_cp,
},
)
node_uuid = (created.get("response") or created).get("uuid", "")
if not node_uuid:
raise RuntimeError(f"Не удалось создать ноду {node_name}")
changes.append(f"created node {node_name} ({target_addr})")
else:
node_uuid = existing_node["uuid"]
cur_cp = existing_node.get("configProfile") or {}
cur_inbounds = [
i["uuid"] if isinstance(i, dict) else i
for i in (cur_cp.get("activeInbounds") or [])
]
needs_update = (
existing_node.get("address") != target_addr
or int(existing_node.get("port") or 0) != int(node_cfg["port"])
or cur_cp.get("activeConfigProfileUuid") != profile_uuid
or cur_inbounds != [inbound_uuid]
)
if needs_update:
payload = {
"uuid": node_uuid,
"address": target_addr,
"port": int(node_cfg["port"]),
"configProfile": desired_cp,
}
code, _ = api.request("PATCH", "/nodes/", payload, allow_error=True)
if not (200 <= code < 300):
api.request("PUT", "/nodes/", payload)
changes.append(f"updated node {node_name} ({target_addr})")
selfsteal = node_cfg["selfsteal_domain"]
_, hosts_resp = api.request("GET", "/hosts/")
hosts = _extract_list(hosts_resp)
existing_host = next((h for h in hosts if h.get("address") == selfsteal), None)
if not existing_host:
host_payload = {
"remark": node_cfg["host_remark"],
"address": selfsteal,
"port": 443,
"path": node_cfg["xhttp_path"],
"sni": selfsteal,
"host": selfsteal,
"fingerprint": "chrome",
"isDisabled": False,
"securityLayer": "DEFAULT",
"alpn": "h2",
"xHttpExtraParams": {
"xmux": {
"maxConcurrency": "16-32",
"hMaxRequestTimes": "600-900",
"hMaxReusableSecs": "1800-3000",
},
"noSSEHeader": True,
"xPaddingBytes": "100-1000",
},
"inbound": {
"configProfileUuid": profile_uuid,
"configProfileInboundUuid": inbound_uuid,
},
"nodes": [node_uuid],
}
if tpl_uuid:
host_payload["xrayJsonTemplateUuid"] = tpl_uuid
_, created = api.request("POST", "/hosts/", host_payload)
host_uuid = (created.get("response") or created).get("uuid", "")
if not host_uuid:
raise RuntimeError(f"Не удалось создать host {selfsteal}")
changes.append(f"created host {selfsteal}")
else:
host_uuid = existing_host["uuid"]
patch_fields: dict[str, Any] = {"uuid": host_uuid}
if existing_host.get("remark") != node_cfg["host_remark"]:
patch_fields["remark"] = node_cfg["host_remark"]
if tpl_uuid and existing_host.get("xrayJsonTemplateUuid") != tpl_uuid:
patch_fields["xrayJsonTemplateUuid"] = tpl_uuid
if len(patch_fields) > 1:
api.request("PATCH", "/hosts/", patch_fields)
changes.append(f"updated host {selfsteal}")
def sync_squad(
api: RemnawaveAPI,
squad_name: str,
inbound_uuids: list[str],
changes: list[str],
) -> dict[str, str]:
_, squads_resp = api.request("GET", "/internal-squads/")
squads = _extract_list(squads_resp, "internalSquads")
squad_map = {s["name"]: s["uuid"] for s in squads if s.get("name") and s.get("uuid")}
target = next((s for s in squads if s.get("name") == squad_name), None)
if target and inbound_uuids:
cur_inbounds = [i["uuid"] for i in (target.get("inbounds") or []) if i.get("uuid")]
missing = [u for u in inbound_uuids if u not in cur_inbounds]
if missing:
merged = list(dict.fromkeys(cur_inbounds + inbound_uuids))
api.request(
"PATCH",
"/internal-squads/",
{"uuid": target["uuid"], "inbounds": merged},
)
changes.append(f"updated squad {squad_name} inbounds")
return squad_map
def sync_subscription_settings(
api: RemnawaveAPI,
sub_page_cfg_name: str,
changes: list[str],
) -> None:
_, cfgs_resp = api.request("GET", "/subscription-page-configs/")
configs = _extract_list(cfgs_resp, "configs")
if not any(c.get("name") == sub_page_cfg_name for c in configs):
api.request("POST", "/subscription-page-configs/", {"name": sub_page_cfg_name})
changes.append(f"created subscription-page-config {sub_page_cfg_name}")
code, settings_resp = api.request("GET", "/subscription-settings/", allow_error=True)
if not (200 <= code < 300) or not isinstance(settings_resp, dict):
return
resp_obj = settings_resp.get("response") or settings_resp
browser_rule = {
"name": "Browser",
"enabled": True,
"operator": "OR",
"description": "IaC: HTML-страница подписки",
"responseType": "BROWSER",
"conditions": [
{"headerName": "accept", "operator": "CONTAINS", "value": "text/html", "caseSensitive": False},
{"headerName": "user-agent", "operator": "CONTAINS", "value": "Mozilla", "caseSensitive": False},
],
}
cur_rules = (resp_obj.get("responseRules") or {}).get("rules") or []
other_rules = [
r
for r in cur_rules
if r.get("name") not in ("Browser", "Browser Subscription")
and r.get("responseType") != "BROWSER"
]
desired_rules = [browser_rule] + other_rules
if (
resp_obj.get("serveJsonAtBaseSubscription") is True
and len(cur_rules) == len(desired_rules)
and cur_rules
and cur_rules[0].get("name") == "Browser"
and cur_rules[0].get("responseType") == "BROWSER"
and cur_rules[0].get("enabled") is True
):
return
patched = dict(resp_obj)
patched["serveJsonAtBaseSubscription"] = True
patched["responseRules"] = dict(resp_obj.get("responseRules") or {})
patched["responseRules"]["rules"] = desired_rules
pcode, _ = api.request("PATCH", "/subscription-settings/", patched, allow_error=True)
if not (200 <= pcode < 300):
api.request("PUT", "/subscription-settings/", patched)
changes.append("updated subscription-settings Browser rule")
def sync_users(
api: RemnawaveAPI,
desired_users: list[dict[str, Any]],
squad_map: dict[str, str],
xray_checker_user: str,
sub_domain: str,
changes: list[str],
) -> str:
code, users_resp = api.request("GET", "/users/?size=500", allow_error=True)
if not (200 <= code < 300):
_, users_resp = api.request("GET", "/users/")
existing_users = _extract_list(users_resp, "users")
by_name = {u["username"]: u for u in existing_users if u.get("username")}
desired_names: set[str] = set()
mutated = False
for u in desired_users:
uname = u["username"]
desired_names.add(uname)
squad_name = u.get("squad", "Default-Squad")
sid = squad_map.get(squad_name, "")
want_squads = [sid] if sid else []
want_body = {
"username": uname,
"status": u.get("status", "ACTIVE"),
"expireAt": u["expire_at"],
"description": u.get("description", ""),
"trafficLimitBytes": int(u.get("traffic_limit_bytes", 0)),
"trafficLimitStrategy": u.get("traffic_limit_strategy", "NO_RESET"),
"activeInternalSquads": want_squads,
}
cur = by_name.get(uname)
if not cur:
api.request("POST", "/users/", want_body)
changes.append(f"created user {uname}")
mutated = True
else:
cur_squads = [
s["uuid"] if isinstance(s, dict) else s
for s in (cur.get("activeInternalSquads") or [])
]
differs = (
cur.get("status") != want_body["status"]
or _norm_iso(cur.get("expireAt")) != _norm_iso(want_body["expireAt"])
or (cur.get("description") or "") != want_body["description"]
or int(cur.get("trafficLimitBytes") or 0) != want_body["trafficLimitBytes"]
or cur.get("trafficLimitStrategy") != want_body["trafficLimitStrategy"]
or cur_squads != want_squads
)
if differs:
api.request("PATCH", "/users/", {**want_body, "uuid": cur["uuid"]})
changes.append(f"updated user {uname}")
mutated = True
for uname, cur in by_name.items():
if uname not in desired_names:
api.request("DELETE", f"/users/{cur['uuid']}")
changes.append(f"deleted user {uname}")
mutated = True
if mutated:
_, users_resp = api.request("GET", "/users/?size=500")
existing_users = _extract_list(users_resp, "users")
by_name = {u["username"]: u for u in existing_users if u.get("username")}
xray_url = ""
if xray_checker_user and xray_checker_user in by_name:
uobj = by_name[xray_checker_user]
xray_url = uobj.get("subscriptionUrl") or ""
if not xray_url and uobj.get("shortUuid") and sub_domain:
xray_url = f"https://{sub_domain}/{uobj['shortUuid']}"
return xray_url
def main() -> int:
if len(sys.argv) != 2:
print("Usage: sync_remnawave.py <sync_config.json>", file=sys.stderr)
return 2
with open(sys.argv[1], "r", encoding="utf-8") as fh:
cfg = json.load(fh)
token = os.environ.get("RW_API_TOKEN", "").strip()
if not token:
print("RW_API_TOKEN is required", file=sys.stderr)
return 1
api = RemnawaveAPI(cfg["panel_domain"], token)
changes: list[str] = []
warnings: list[str] = []
nb_peers = {}
if cfg.get("netbird_connected"):
nb_peers = resolve_netbird_peers(
cfg.get("netbird_peers_url", ""),
os.environ.get("NB_API_TOKEN", "").strip(),
)
tpl_uuid = sync_xray_template(
api,
cfg["xray_template"]["name"],
cfg["xray_template_json"],
changes,
)
inbound_uuids: list[str] = []
for node_cfg in cfg.get("nodes", []):
profile_uuid, inbound_uuid = ensure_profile_and_inbound(
api, node_cfg, cfg.get("profile_templates", {}), changes
)
inbound_uuids.append(inbound_uuid)
sync_node_and_host(
api,
node_cfg,
profile_uuid,
inbound_uuid,
tpl_uuid,
nb_peers,
bool(cfg.get("netbird_connected")),
changes,
warnings,
)
squad_map = sync_squad(api, cfg.get("squad_name", "Default-Squad"), inbound_uuids, changes)
sync_subscription_settings(api, cfg.get("subscription_page_config", "Homelab"), changes)
xray_url = sync_users(
api,
cfg.get("users", []),
squad_map,
cfg.get("xray_checker_user", "homelab"),
cfg.get("sub_domain", ""),
changes,
)
result = {
"changed": len(changes) > 0,
"changes": changes,
"warnings": warnings,
"xray_checker_subscription_url": xray_url,
}
print(json.dumps(result, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,372 @@
---
- name: Проверить наличие Infisical CLI-контекста для записи секретов
ansible.builtin.assert:
that:
- lookup('env', 'INFISICAL_TOKEN') | length > 0
- lookup('env', 'INFISICAL_DOMAIN') | length > 0
- lookup('env', 'INFISICAL_PROJECT_ID') | length > 0
fail_msg: "Для роли remnawave нужны INFISICAL_TOKEN / INFISICAL_DOMAIN / INFISICAL_PROJECT_ID (деплой через deploy-machine.sh)."
- name: Проверить имя администратора панели
ansible.builtin.assert:
that:
- remnawave_admin_username | length > 0
fail_msg: "Нужен REMNAWAVE_ADMIN_USER или ADMIN_USER."
# Remnawave v3: /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{24,}$/
- name: Запомнить, какие секреты панели нужно сгенерировать
ansible.builtin.set_fact:
remnawave_need_admin_password: "{{ (remnawave_admin_password | length < 24) or (remnawave_admin_password is not match('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{24,}$')) }}"
remnawave_need_app_secret: "{{ remnawave_app_secret | length == 0 }}"
remnawave_need_postgres_password: "{{ remnawave_postgres_password | length == 0 }}"
remnawave_need_metrics_pass: "{{ remnawave_metrics_pass | length == 0 }}"
remnawave_need_webhook_secret: "{{ remnawave_webhook_secret | length == 0 }}"
remnawave_need_node_secret: "{{ lookup('env', 'REMNANODE_SECRET_KEY', default='') | length == 0 }}"
- name: Сгенерировать отсутствующие секреты панели
ansible.builtin.set_fact:
remnawave_admin_password: "{{ remnawave_admin_password if not remnawave_need_admin_password else ('Rw9' ~ lookup('password', '/dev/null length=29 chars=ascii_letters,digits')) }}"
remnawave_app_secret: "{{ remnawave_app_secret if not remnawave_need_app_secret else lookup('password', '/dev/null length=64 chars=hexdigits') }}"
remnawave_postgres_password: "{{ remnawave_postgres_password if not remnawave_need_postgres_password else lookup('password', '/dev/null length=48 chars=hexdigits') }}"
remnawave_metrics_pass: "{{ remnawave_metrics_pass if not remnawave_need_metrics_pass else lookup('password', '/dev/null length=64 chars=hexdigits') }}"
remnawave_webhook_secret: "{{ remnawave_webhook_secret if not remnawave_need_webhook_secret else lookup('password', '/dev/null length=64 chars=ascii_letters,digits') }}"
remnawave_secrets_generated: >-
{{
remnawave_need_admin_password or
remnawave_need_app_secret or
remnawave_need_postgres_password or
remnawave_need_metrics_pass or
remnawave_need_webhook_secret
}}
no_log: true
- name: Создать каталоги Remnawave
ansible.builtin.file:
path: "{{ item }}"
state: directory
owner: root
group: root
mode: "0755"
loop:
- "{{ remnawave_dir }}"
- "{{ remnawave_subscription_dir }}"
- name: Поставить curl, jq, openssl и python3
ansible.builtin.apt:
name:
- curl
- jq
- openssl
- python3
state: present
- name: Записать .env панели
ansible.builtin.template:
src: env.j2
dest: "{{ remnawave_dir }}/.env"
owner: root
group: root
mode: "0600"
no_log: true
register: remnawave_env_file
- name: Создать папку /services/remnawave в Infisical при необходимости
ansible.builtin.shell: |
set -euo pipefail
infisical secrets folders create \
--domain="${INFISICAL_DOMAIN}" \
--token="${INFISICAL_TOKEN}" \
--env=prod \
--projectId="${INFISICAL_PROJECT_ID}" \
--path="/services" \
--name="remnawave" \
--silent 2>/dev/null || true
args:
executable: /bin/bash
delegate_to: localhost
become: false
environment:
INFISICAL_DOMAIN: "{{ lookup('env', 'INFISICAL_DOMAIN') }}"
INFISICAL_TOKEN: "{{ lookup('env', 'INFISICAL_TOKEN') }}"
INFISICAL_PROJECT_ID: "{{ lookup('env', 'INFISICAL_PROJECT_ID') }}"
when: remnawave_secrets_generated | bool
changed_when: false
- name: Сохранить сгенерированные секреты панели в Infisical
ansible.builtin.shell: |
set -euo pipefail
out=$(infisical secrets set \
--domain="${INFISICAL_DOMAIN}" \
--token="${INFISICAL_TOKEN}" \
--env=prod \
--projectId="${INFISICAL_PROJECT_ID}" \
--path="/services/remnawave" \
"REMNAWAVE_ADMIN_USER=${RW_ADMIN_USER}" \
"REMNAWAVE_ADMIN_PASSWORD=${RW_ADMIN_PASS}" \
"REMNAWAVE_APP_SECRET=${RW_APP_SECRET}" \
"REMNAWAVE_POSTGRES_PASSWORD=${RW_PG_PASS}" \
"REMNAWAVE_METRICS_PASS=${RW_METRICS_PASS}" \
"REMNAWAVE_WEBHOOK_SECRET_HEADER=${RW_WEBHOOK}" \
--silent 2>&1) || {
echo "infisical secrets set failed: ${out}" >&2
exit 1
}
args:
executable: /bin/bash
delegate_to: localhost
become: false
environment:
INFISICAL_DOMAIN: "{{ lookup('env', 'INFISICAL_DOMAIN') }}"
INFISICAL_TOKEN: "{{ lookup('env', 'INFISICAL_TOKEN') }}"
INFISICAL_PROJECT_ID: "{{ lookup('env', 'INFISICAL_PROJECT_ID') }}"
RW_ADMIN_USER: "{{ remnawave_admin_username }}"
RW_ADMIN_PASS: "{{ remnawave_admin_password }}"
RW_APP_SECRET: "{{ remnawave_app_secret }}"
RW_PG_PASS: "{{ remnawave_postgres_password }}"
RW_METRICS_PASS: "{{ remnawave_metrics_pass }}"
RW_WEBHOOK: "{{ remnawave_webhook_secret }}"
when: remnawave_secrets_generated | bool
changed_when: true
no_log: true
- name: Записать compose панели
ansible.builtin.template:
src: compose.yaml.j2
dest: "{{ remnawave_dir }}/compose.yaml"
owner: root
group: root
mode: "0644"
- name: Стянуть образы панели Remnawave
ansible.builtin.command: docker compose -f {{ remnawave_dir }}/compose.yaml pull
register: remnawave_compose_pull
changed_when: "'Pulled' in remnawave_compose_pull.stderr or 'Downloaded' in remnawave_compose_pull.stderr"
retries: 3
delay: 10
until: remnawave_compose_pull.rc == 0
- name: Запустить панель Remnawave
ansible.builtin.command: docker compose -f {{ remnawave_dir }}/compose.yaml up -d --remove-orphans
register: remnawave_compose_up
changed_when: >-
'Started' in remnawave_compose_up.stderr or
'Created' in remnawave_compose_up.stderr or
'Recreated' in remnawave_compose_up.stderr or
'Pulled' in remnawave_compose_up.stderr
- name: Дождаться health панели
ansible.builtin.uri:
url: "http://127.0.0.1:{{ remnawave_metrics_port }}/health"
status_code: 200
register: remnawave_health
retries: 40
delay: 3
until: remnawave_health.status == 200
- name: Дождаться HTTPS панели через Traefik
ansible.builtin.command: >
curl -fsSk --resolve {{ remnawave_panel_domain }}:443:127.0.0.1
https://{{ remnawave_panel_domain }}/api/auth/status
register: remnawave_auth_status
changed_when: false
retries: 30
delay: 5
until: remnawave_auth_status.rc == 0
- name: Инициализировать админа, API-токен (если невалиден) и SECRET_KEY ноды
ansible.builtin.shell: |
set -euo pipefail
API_BASE="https://${RW_PANEL_DOMAIN}/api"
CURL=(curl -sSk --resolve "${RW_PANEL_DOMAIN}:443:127.0.0.1" -H "X-Remnawave-Client-Type: browser")
AUTH_HDR=()
admin_created=0
token_updated=0
node_secret_created=0
active_token="${RW_API_TOKEN:-}"
node_secret="${RW_NODE_SECRET:-}"
redact_json() {
jq 'walk(
if type == "object" then
with_entries(
if (.key | test("(?i)privateKey|privKey|secretKey|SECRET_KEY|accessToken|apiToken|password|^token$"))
then .value = "[REDACTED]"
else . end
)
else . end
)' 2>/dev/null || echo '[body redacted]'
}
api() {
local method="$1"; shift
local path="$1"; shift
local tmp code
tmp=$(mktemp)
if [ -n "${AUTH_HDR[*]:-}" ]; then
code=$("${CURL[@]}" -o "$tmp" -w '%{http_code}' -X "$method" "${AUTH_HDR[@]}" \
-H "Content-Type: application/json" "${API_BASE}${path}" "$@") || true
else
code=$("${CURL[@]}" -o "$tmp" -w '%{http_code}' -X "$method" \
-H "Content-Type: application/json" "${API_BASE}${path}" "$@") || true
fi
if [[ ! "${code}" =~ ^2 ]]; then
echo "API ${method} ${path} -> HTTP ${code}" >&2
redact_json < "$tmp" >&2 || true
rm -f "$tmp"
return 1
fi
cat "$tmp"
rm -f "$tmp"
}
status_json=$("${CURL[@]}" "${API_BASE}/auth/status")
is_register=$(printf '%s' "${status_json}" | jq -r '.response.isRegisterAllowed // .isRegisterAllowed // false')
token_valid=0
if [ "${is_register}" != "true" ] && [ -n "${active_token}" ]; then
tcode=$("${CURL[@]}" -o /dev/null -w '%{http_code}' \
-H "Authorization: Bearer ${active_token}" "${API_BASE}/nodes/" || true)
if [ "${tcode}" = "200" ]; then
token_valid=1
fi
fi
if [ "${is_register}" = "true" ]; then
reg_payload=$(jq -n --arg u "$RW_ADMIN_USER" --arg p "$RW_ADMIN_PASS" '{username: $u, password: $p}')
reg_resp=$(api POST /auth/register -d "${reg_payload}")
access=$(printf '%s' "${reg_resp}" | jq -r '.response.accessToken // .accessToken // empty')
if [ -z "${access}" ]; then
echo "Не удалось зарегистрировать админа Remnawave" >&2
exit 1
fi
AUTH_HDR=(-H "Authorization: Bearer ${access}")
admin_created=1
elif [ "${token_valid}" -eq 0 ] || [ -z "${node_secret}" ]; then
login_payload=$(jq -n --arg u "$RW_ADMIN_USER" --arg p "$RW_ADMIN_PASS" '{username: $u, password: $p}')
login_resp=$(api POST /auth/login -d "${login_payload}")
access=$(printf '%s' "${login_resp}" | jq -r '.response.accessToken // .accessToken // empty')
if [ -z "${access}" ]; then
echo "Не удалось войти в панель Remnawave" >&2
exit 1
fi
AUTH_HDR=(-H "Authorization: Bearer ${access}")
fi
if [ "${token_valid}" -eq 0 ]; then
tokens_json=$(api GET /tokens/ || echo '{"response":[]}')
old_uuids=$(printf '%s' "${tokens_json}" | jq -r --arg n "$RW_TOKEN_NAME" '
(.response // .)
| if type=="array" then . elif .tokens then .tokens else [] end
| map(select(.name == $n or .tokenName == $n) | .uuid)
| .[]')
for old_uuid in ${old_uuids}; do
[ -n "${old_uuid}" ] || continue
api DELETE "/tokens/${old_uuid}" >/dev/null || true
done
scopes_json=$(api GET /tokens/scopes || echo '{"response":{"wildcard":"*"}}')
wildcard=$(printf '%s' "${scopes_json}" | jq -r '.response.wildcard // "*"')
token_payload=$(jq -n --arg n "$RW_TOKEN_NAME" --arg s "${wildcard:-*}" \
'{name: $n, expiresInDays: 3650, scopes: [$s]}')
token_resp=$(api POST /tokens/ -d "${token_payload}")
created=$(printf '%s' "${token_resp}" | jq -r '
.response.token // .response.apiToken // .token // .apiToken // empty')
if [ -z "${created}" ]; then
echo "Не удалось создать API-токен Remnawave" >&2
exit 1
fi
active_token="${created}"
token_updated=1
fi
if [ -z "${node_secret}" ]; then
keygen_resp=$(api GET /keygen/)
node_secret=$(printf '%s' "${keygen_resp}" | jq -r '.response.secretKey // .secretKey // empty')
if [ -z "${node_secret}" ]; then
echo "Не удалось получить SECRET_KEY ноды из /api/keygen/" >&2
exit 1
fi
node_secret_created=1
fi
umask 077
jq -n --arg api_token "${active_token}" --arg node_secret "${node_secret}" \
'{api_token: $api_token, node_secret: $node_secret}' > "${RW_SECRETS_FILE}"
chmod 600 "${RW_SECRETS_FILE}"
jq -n \
--argjson admin_created "${admin_created}" \
--argjson token_updated "${token_updated}" \
--argjson node_secret_created "${node_secret_created}" \
'{
admin_created: ($admin_created == 1),
token_updated: ($token_updated == 1),
node_secret_created: ($node_secret_created == 1),
changed: (($admin_created + $token_updated + $node_secret_created) > 0)
}'
args:
executable: /bin/bash
environment:
RW_PANEL_DOMAIN: "{{ remnawave_panel_domain }}"
RW_ADMIN_USER: "{{ remnawave_admin_username }}"
RW_ADMIN_PASS: "{{ remnawave_admin_password }}"
RW_API_TOKEN: "{{ remnawave_api_token }}"
RW_NODE_SECRET: "{{ lookup('env', 'REMNANODE_SECRET_KEY', default='') }}"
RW_TOKEN_NAME: "{{ remnawave_api_token_name }}"
RW_SECRETS_FILE: "{{ remnawave_dir }}/.bootstrap-secrets.json"
register: remnawave_bootstrap_raw
changed_when: (remnawave_bootstrap_raw.stdout | from_json).changed
no_log: true
- name: Разобрать статус bootstrap
ansible.builtin.set_fact:
remnawave_bootstrap: "{{ remnawave_bootstrap_raw.stdout | from_json }}"
- name: Прочитать API-токен и SECRET_KEY
ansible.builtin.slurp:
src: "{{ remnawave_dir }}/.bootstrap-secrets.json"
register: remnawave_bootstrap_secrets
no_log: true
- name: Подставить секреты bootstrap в факты
ansible.builtin.set_fact:
remnawave_api_token: "{{ (remnawave_bootstrap_secrets.content | b64decode | from_json).api_token }}"
remnawave_node_secret: "{{ (remnawave_bootstrap_secrets.content | b64decode | from_json).node_secret }}"
no_log: true
- name: Записать .env subscription-page
ansible.builtin.template:
src: subscription.env.j2
dest: "{{ remnawave_subscription_dir }}/.env"
owner: root
group: root
mode: "0600"
no_log: true
register: remnawave_sub_env
- name: Записать compose subscription-page
ansible.builtin.template:
src: subscription-compose.yaml.j2
dest: "{{ remnawave_subscription_dir }}/compose.yaml"
owner: root
group: root
mode: "0644"
register: remnawave_sub_compose
- name: Запустить subscription-page
ansible.builtin.command: docker compose -f {{ remnawave_subscription_dir }}/compose.yaml up -d --remove-orphans
register: remnawave_sub_up
changed_when: >-
'Started' in remnawave_sub_up.stderr or
'Created' in remnawave_sub_up.stderr or
'Recreated' in remnawave_sub_up.stderr or
'Pulled' in remnawave_sub_up.stderr
- name: Дождаться subscription-page (docker health)
ansible.builtin.shell: |
docker inspect remnawave-subscription-page --format '{% raw %}{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}{% endraw %}'
register: remnawave_sub_health
changed_when: false
retries: 30
delay: 2
until: (remnawave_sub_health.stdout | trim) == 'healthy'
+19 -335
View File
@@ -1,297 +1,22 @@
---
- name: Проверить наличие Infisical CLI-контекста для записи секретов
ansible.builtin.assert:
that:
- lookup('env', 'INFISICAL_TOKEN') | length > 0
- lookup('env', 'INFISICAL_DOMAIN') | length > 0
- lookup('env', 'INFISICAL_PROJECT_ID') | length > 0
fail_msg: "Для роли remnawave нужны INFISICAL_TOKEN / INFISICAL_DOMAIN / INFISICAL_PROJECT_ID (деплой через deploy-machine.sh)."
- name: Развернуть и инициализировать панель Remnawave
ansible.builtin.include_tasks: bootstrap.yml
- name: Проверить имя администратора панели
ansible.builtin.assert:
that:
- remnawave_admin_username | length > 0
fail_msg: "Нужен REMNAWAVE_ADMIN_USER или ADMIN_USER."
- name: Синхронизировать ноды, профили, шаблоны и пользователей Remnawave
ansible.builtin.include_tasks: sync.yml
# Remnawave v3: /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{24,}$/
- name: Запомнить, какие секреты панели нужно сгенерировать
- name: Определить изменившиеся секреты для Infisical
ansible.builtin.set_fact:
remnawave_need_admin_password: "{{ (remnawave_admin_password | length < 24) or (remnawave_admin_password is not match('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{24,}$')) }}"
remnawave_need_app_secret: "{{ remnawave_app_secret | length == 0 }}"
remnawave_need_postgres_password: "{{ remnawave_postgres_password | length == 0 }}"
remnawave_need_metrics_pass: "{{ remnawave_metrics_pass | length == 0 }}"
remnawave_need_webhook_secret: "{{ remnawave_webhook_secret | length == 0 }}"
remnawave_need_node_secret: "{{ lookup('env', 'REMNANODE_SECRET_KEY', default='') | length == 0 }}"
- name: Сгенерировать отсутствующие секреты панели
ansible.builtin.set_fact:
remnawave_admin_password: "{{ remnawave_admin_password if not remnawave_need_admin_password else ('Rw9' ~ lookup('password', '/dev/null length=29 chars=ascii_letters,digits')) }}"
remnawave_app_secret: "{{ remnawave_app_secret if not remnawave_need_app_secret else lookup('password', '/dev/null length=64 chars=hexdigits') }}"
remnawave_postgres_password: "{{ remnawave_postgres_password if not remnawave_need_postgres_password else lookup('password', '/dev/null length=48 chars=hexdigits') }}"
remnawave_metrics_pass: "{{ remnawave_metrics_pass if not remnawave_need_metrics_pass else lookup('password', '/dev/null length=64 chars=hexdigits') }}"
remnawave_webhook_secret: "{{ remnawave_webhook_secret if not remnawave_need_webhook_secret else lookup('password', '/dev/null length=64 chars=ascii_letters,digits') }}"
remnawave_current_xray_url: "{{ lookup('env', 'XRAY_CHECKER_SUBSCRIPTION_URL', default='') }}"
remnawave_need_infisical_sync: >-
{{
(remnawave_bootstrap.token_updated | default(false)) or
(remnawave_bootstrap.node_secret_created | default(false)) or
((remnawave_xray_checker_url | default('') | length > 0) and (remnawave_xray_checker_url != lookup('env', 'XRAY_CHECKER_SUBSCRIPTION_URL', default='')))
}}
no_log: true
- name: Создать каталоги Remnawave
ansible.builtin.file:
path: "{{ item }}"
state: directory
owner: root
group: root
mode: "0755"
loop:
- "{{ remnawave_dir }}"
- "{{ remnawave_subscription_dir }}"
- name: Поставить curl, jq и openssl
ansible.builtin.apt:
name:
- curl
- jq
- openssl
state: present
- name: Записать .env панели
ansible.builtin.template:
src: env.j2
dest: "{{ remnawave_dir }}/.env"
owner: root
group: root
mode: "0600"
no_log: true
- name: Создать папку /services/remnawave в Infisical при необходимости
ansible.builtin.shell: |
set -euo pipefail
infisical secrets folders create \
--domain="${INFISICAL_DOMAIN}" \
--token="${INFISICAL_TOKEN}" \
--env=prod \
--projectId="${INFISICAL_PROJECT_ID}" \
--path="/services" \
--name="remnawave" \
--silent 2>/dev/null || true
args:
executable: /bin/bash
delegate_to: localhost
become: false
environment:
INFISICAL_DOMAIN: "{{ lookup('env', 'INFISICAL_DOMAIN') }}"
INFISICAL_TOKEN: "{{ lookup('env', 'INFISICAL_TOKEN') }}"
INFISICAL_PROJECT_ID: "{{ lookup('env', 'INFISICAL_PROJECT_ID') }}"
changed_when: false
- name: Сохранить сгенерированные секреты панели в Infisical
ansible.builtin.shell: |
set -euo pipefail
out=$(infisical secrets set \
--domain="${INFISICAL_DOMAIN}" \
--token="${INFISICAL_TOKEN}" \
--env=prod \
--projectId="${INFISICAL_PROJECT_ID}" \
--path="/services/remnawave" \
"REMNAWAVE_ADMIN_USER=${RW_ADMIN_USER}" \
"REMNAWAVE_ADMIN_PASSWORD=${RW_ADMIN_PASS}" \
"REMNAWAVE_APP_SECRET=${RW_APP_SECRET}" \
"REMNAWAVE_POSTGRES_PASSWORD=${RW_PG_PASS}" \
"REMNAWAVE_METRICS_PASS=${RW_METRICS_PASS}" \
"REMNAWAVE_WEBHOOK_SECRET_HEADER=${RW_WEBHOOK}" \
--silent 2>&1) || {
echo "infisical secrets set failed: ${out}" >&2
exit 1
}
args:
executable: /bin/bash
delegate_to: localhost
become: false
environment:
INFISICAL_DOMAIN: "{{ lookup('env', 'INFISICAL_DOMAIN') }}"
INFISICAL_TOKEN: "{{ lookup('env', 'INFISICAL_TOKEN') }}"
INFISICAL_PROJECT_ID: "{{ lookup('env', 'INFISICAL_PROJECT_ID') }}"
RW_ADMIN_USER: "{{ remnawave_admin_username }}"
RW_ADMIN_PASS: "{{ remnawave_admin_password }}"
RW_APP_SECRET: "{{ remnawave_app_secret }}"
RW_PG_PASS: "{{ remnawave_postgres_password }}"
RW_METRICS_PASS: "{{ remnawave_metrics_pass }}"
RW_WEBHOOK: "{{ remnawave_webhook_secret }}"
# Всегда пишем admin+runtime секреты в /services/remnawave (идемпотентно).
changed_when: true
no_log: true
- name: Записать compose панели
ansible.builtin.template:
src: compose.yaml.j2
dest: "{{ remnawave_dir }}/compose.yaml"
owner: root
group: root
mode: "0644"
- name: Положить шаблон config profile DE-Yali
ansible.builtin.copy:
src: profile-de-yali.json
dest: "{{ remnawave_dir }}/profile-de-yali.json"
owner: root
group: root
mode: "0644"
- name: Положить клиентский шаблон Xray Except-RU
ansible.builtin.copy:
src: xray-except-ru.json
dest: "{{ remnawave_dir }}/xray-except-ru.json"
owner: root
group: root
mode: "0644"
- name: Стянуть образы панели Remnawave
ansible.builtin.command: docker compose -f {{ remnawave_dir }}/compose.yaml pull
register: remnawave_compose_pull
changed_when: remnawave_compose_pull.rc == 0
retries: 3
delay: 10
until: remnawave_compose_pull.rc == 0
- name: Запустить панель Remnawave
ansible.builtin.command: docker compose -f {{ remnawave_dir }}/compose.yaml up -d --remove-orphans
register: remnawave_compose_up
changed_when: >-
'Started' in remnawave_compose_up.stderr or
'Created' in remnawave_compose_up.stderr or
'Recreated' in remnawave_compose_up.stderr or
'Pulled' in remnawave_compose_up.stderr
- name: Дождаться health панели
ansible.builtin.uri:
url: "http://127.0.0.1:{{ remnawave_metrics_port }}/health"
status_code: 200
register: remnawave_health
retries: 40
delay: 3
until: remnawave_health.status == 200
- name: Дождаться HTTPS панели через Traefik
ansible.builtin.command: >
curl -fsSk --resolve {{ remnawave_panel_domain }}:443:127.0.0.1
https://{{ remnawave_panel_domain }}/api/auth/status
register: remnawave_auth_status
changed_when: false
retries: 30
delay: 5
until: remnawave_auth_status.rc == 0
- name: Записать список пользователей
ansible.builtin.copy:
dest: "{{ remnawave_dir }}/users.json"
content: "{{ lookup('file', 'users.yml') | from_yaml | to_json }}"
owner: root
group: root
mode: "0600"
- name: Проверить, что панель сама в NetBird
ansible.builtin.command: netbird status
register: remnawave_local_netbird
changed_when: false
failed_when: false
- name: Узнать NetBird IP ноды
ansible.builtin.shell: |
set -euo pipefail
if [ -z "${NB_API_TOKEN:-}" ]; then
exit 0
fi
curl -fsS --max-time 20 -H "Authorization: Token ${NB_API_TOKEN}" \
"{{ remnawave_netbird_peers_url }}" \
| jq -r --arg h "$NB_HOST" '
(if type == "array" then . elif type == "object" then (.items // .peers // []) else [] end)
| map(select((.hostname // "") == $h or (.name // "") == $h or (.dns_label // "") == $h))
| .[0].ip // empty'
args:
executable: /bin/bash
environment:
NB_API_TOKEN: "{{ lookup('env', 'NETBIRD_API_TOKEN', default='') }}"
NB_HOST: "{{ remnawave_node_netbird_hostname }}"
register: remnawave_node_nb
changed_when: false
failed_when: false
- name: Подставить NetBird-адрес ноды
ansible.builtin.set_fact:
remnawave_node_address: "{{ remnawave_node_nb.stdout | trim }}"
when:
- remnawave_node_nb.stdout | default('') | trim is match('^[0-9.]+$')
- "'Management: Connected' in (remnawave_local_netbird.stdout | default(''))"
- name: Peer ноды ещё не в NetBird
ansible.builtin.debug:
msg: "Адрес ноды остаётся {{ remnawave_node_address }}: панель ещё не в NetBird или peer {{ remnawave_node_netbird_hostname }} не найден."
when: >-
(remnawave_node_nb.stdout | default('') | trim is not match('^[0-9.]+$')) or
('Management: Connected' not in (remnawave_local_netbird.stdout | default('')))
- name: Инициализировать админа, API-токен, профиль DE-Yali, ноду и хост
ansible.builtin.shell: |
set -euo pipefail
{{ lookup('template', 'bootstrap-panel.sh.j2') }}
args:
executable: /bin/bash
environment:
RW_ADMIN_USER: "{{ remnawave_admin_username }}"
RW_ADMIN_PASS: "{{ remnawave_admin_password }}"
RW_API_TOKEN: "{{ remnawave_api_token }}"
RW_NODE_SECRET: "{{ lookup('env', 'REMNANODE_SECRET_KEY', default='') }}"
RW_TOKEN_NAME: "{{ remnawave_api_token_name }}"
RW_PROFILE_NAME: "{{ remnawave_profile_name }}"
RW_INBOUND_TAG: "{{ remnawave_inbound_tag }}"
RW_NODE_NAME: "{{ remnawave_node_name }}"
RW_NODE_ADDR: "{{ remnawave_node_address }}"
RW_NODE_PORT: "{{ remnawave_node_port }}"
RW_SUB_DOMAIN: "{{ remnawave_sub_domain }}"
RW_XRAY_USER: "{{ remnawave_xray_checker_user }}"
RW_NODE_CC: "{{ remnawave_node_country }}"
RW_SELFSTEAL: "{{ remnawave_selfsteal_domain }}"
RW_XHTTP_PATH: "{{ remnawave_xhttp_path }}"
RW_HOST_REMARK: "{{ remnawave_host_remark }}"
RW_XRAY_TEMPLATE_NAME: "{{ remnawave_xray_template_name }}"
RW_XRAY_TEMPLATE_FILE: "{{ remnawave_dir }}/xray-except-ru.json"
RW_PROFILE_FILE: "{{ remnawave_dir }}/profile-de-yali.json"
RW_PANEL_DOMAIN: "{{ remnawave_panel_domain }}"
RW_USERS_FILE: "{{ remnawave_dir }}/users.json"
RW_SECRETS_FILE: "{{ remnawave_dir }}/.bootstrap-secrets.json"
register: remnawave_bootstrap_raw
changed_when: false
failed_when: remnawave_bootstrap_raw.rc != 0
- name: Разобрать статус bootstrap
ansible.builtin.set_fact:
remnawave_bootstrap: "{{ remnawave_bootstrap_raw.stdout | from_json }}"
- name: Прочитать API-токен и SECRET_KEY (файл не попадает в лог)
ansible.builtin.slurp:
src: "{{ remnawave_dir }}/.bootstrap-secrets.json"
register: remnawave_bootstrap_secrets
no_log: true
- name: Подставить секреты bootstrap в факты
ansible.builtin.set_fact:
remnawave_api_token: "{{ (remnawave_bootstrap_secrets.content | b64decode | from_json).api_token }}"
remnawave_node_secret: "{{ (remnawave_bootstrap_secrets.content | b64decode | from_json).node_secret }}"
remnawave_xray_checker_url: "{{ (remnawave_bootstrap_secrets.content | b64decode | from_json).xray_checker_subscription_url | default('') }}"
no_log: true
- name: Проверить, что SECRET_KEY ноды получен
ansible.builtin.assert:
that:
- (remnawave_node_secret | default('') | length > 0) or (lookup('env', 'REMNANODE_SECRET_KEY', default='') | length > 0)
fail_msg: "Панель не вернула SECRET_KEY ноды (GET /api/keygen/). Проверьте версию Remnawave и повторите прогон."
- name: Проверить наличие API-токена
ansible.builtin.assert:
that:
- remnawave_api_token | default('') | length > 0
fail_msg: >-
Нет REMNAWAVE_API_TOKEN. Создайте в UI панели (Remnawave Settings → API Tokens,
scopes=*), положите в Infisical /services/remnawave и перезапустите ansible-core-net.
- name: Сохранить API-токен и SECRET_KEY ноды в Infisical
- name: Сохранить API-токен, SECRET_KEY ноды и ссылку Xray Checker в Infisical при изменении
ansible.builtin.shell: |
set -euo pipefail
args=(
@@ -320,12 +45,12 @@
RW_API_TOKEN: "{{ remnawave_api_token }}"
RW_NODE_SECRET: "{{ remnawave_node_secret | default(lookup('env', 'REMNANODE_SECRET_KEY', default='')) }}"
RW_XRAY_URL: "{{ remnawave_xray_checker_url | default('') }}"
when: remnawave_api_token | default('') | length > 0
when:
- remnawave_api_token | default('') | length > 0
- remnawave_need_infisical_sync | bool
changed_when: true
no_log: true
# Токен перевыпускается каждым прогоном, поэтому регистрация в Agent Vault
# перезаписывает креденшл тем же шагом: иначе агент останется с протухшим.
- name: Зарегистрировать панель Remnawave в Agent Vault
ansible.builtin.include_role:
name: agent_vault_service
@@ -341,54 +66,13 @@
agent_vault_secret_key: REMNAWAVE_API_TOKEN
when: remnawave_api_token | default('') | length > 0
- name: Записать .env subscription-page
ansible.builtin.template:
src: subscription.env.j2
dest: "{{ remnawave_subscription_dir }}/.env"
owner: root
group: root
mode: "0600"
no_log: true
- name: Записать compose subscription-page
ansible.builtin.template:
src: subscription-compose.yaml.j2
dest: "{{ remnawave_subscription_dir }}/compose.yaml"
owner: root
group: root
mode: "0644"
- name: Запустить subscription-page
ansible.builtin.command: docker compose -f {{ remnawave_subscription_dir }}/compose.yaml up -d --force-recreate --remove-orphans
register: remnawave_sub_up
changed_when: >-
'Started' in remnawave_sub_up.stderr or
'Created' in remnawave_sub_up.stderr or
'Recreated' in remnawave_sub_up.stderr or
'Pulled' in remnawave_sub_up.stderr
# Корень / за ProxyCheck рвёт коннект без proxy-заголовков; через Traefik
# без shortUuid тоже 502. Официальный probe — /internal/health внутри контейнера
# (docker HEALTHCHECK). С хоста проверяем State.Health.Status.
- name: Дождаться subscription-page (docker health)
ansible.builtin.shell: |
docker inspect remnawave-subscription-page --format '{% raw %}{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}{% endraw %}'
register: remnawave_sub_health
changed_when: false
retries: 30
delay: 2
until: (remnawave_sub_health.stdout | trim) == 'healthy'
- name: Итог Remnawave panel
ansible.builtin.debug:
msg:
- "Панель: https://{{ remnawave_panel_domain }}"
- "Подписка: https://{{ remnawave_sub_domain }}"
- "Админ-логин в Infisical /services/remnawave: REMNAWAVE_ADMIN_USER={{ remnawave_admin_username }}"
- "Админ-пароль сгенерирован в этом прогоне: {{ remnawave_need_admin_password }}"
- "Админ создан в этом прогоне: {{ remnawave_bootstrap.admin_created | default(false) }}"
- "API-токен обновлён в Infisical: {{ remnawave_bootstrap.token_updated | default(false) }}"
- "Профиль {{ remnawave_profile_name }}: {{ remnawave_bootstrap.profile_uuid | default('n/a') }}"
- "Нода {{ remnawave_node_name }}: {{ remnawave_bootstrap.node_uuid | default('n/a') }} адрес {{ remnawave_bootstrap.node_address | default(remnawave_node_address) }}"
- "Ссылка Xray Checker записана в Infisical: {{ remnawave_bootstrap.xray_checker_url_saved | default(false) }}"
- "Host {{ remnawave_selfsteal_domain }}: {{ remnawave_bootstrap.host_uuid | default('n/a') }}"
- "SECRET_KEY ноды получен в этом прогоне: {{ remnawave_bootstrap.node_secret_created | default(false) }}"
- "Изменения sync (nodes.yml / users.yml): {{ remnawave_sync.changes | default([]) }}"
- "Предупреждения sync: {{ remnawave_sync.warnings | default([]) }}"
@@ -0,0 +1,59 @@
---
- name: Загрузить декларативную конфигурацию нод и пользователей Remnawave
ansible.builtin.set_fact:
remnawave_nodes_cfg: "{{ lookup('file', role_path ~ '/files/nodes.yml') | from_yaml }}"
remnawave_users_cfg: "{{ lookup('file', role_path ~ '/files/users.yml') | from_yaml }}"
- name: Проверить, что панель сама в NetBird
ansible.builtin.command: netbird status
register: remnawave_local_netbird
changed_when: false
failed_when: false
- name: Собрать конфигурацию синхронизации Remnawave
ansible.builtin.set_fact:
remnawave_sync_payload:
panel_domain: "{{ remnawave_panel_domain }}"
sub_domain: "{{ remnawave_sub_domain }}"
netbird_peers_url: "{{ remnawave_netbird_peers_url }}"
netbird_connected: "{{ 'Management: Connected' in (remnawave_local_netbird.stdout | default('')) }}"
xray_checker_user: "{{ remnawave_xray_checker_user }}"
xray_template: "{{ remnawave_nodes_cfg.xray_template }}"
xray_template_json: "{{ lookup('file', role_path ~ '/files/' ~ remnawave_nodes_cfg.xray_template.file) | from_json }}"
squad_name: "{{ remnawave_nodes_cfg.squad_name | default('Default-Squad') }}"
subscription_page_config: "{{ remnawave_nodes_cfg.subscription_page_config | default('Homelab') }}"
nodes: "{{ remnawave_nodes_cfg.nodes | default([]) }}"
users: "{{ remnawave_users_cfg.users | default([]) }}"
profile_templates:
profile-selfsteal-xhttp.json: "{{ lookup('file', role_path ~ '/files/profile-selfsteal-xhttp.json') }}"
- name: Положить скрипт синхронизации Remnawave
ansible.builtin.copy:
src: sync_remnawave.py
dest: "{{ remnawave_dir }}/sync_remnawave.py"
owner: root
group: root
mode: "0700"
- name: Записать конфигурацию синхронизации Remnawave
ansible.builtin.copy:
dest: "{{ remnawave_dir }}/sync_config.json"
content: "{{ remnawave_sync_payload | to_json }}"
owner: root
group: root
mode: "0600"
- name: Синхронизировать шаблоны, профили, ноды, хосты и пользователей Remnawave
ansible.builtin.command: "python3 {{ remnawave_dir }}/sync_remnawave.py {{ remnawave_dir }}/sync_config.json"
environment:
RW_API_TOKEN: "{{ remnawave_api_token }}"
NB_API_TOKEN: "{{ lookup('env', 'NETBIRD_API_TOKEN', default='') }}"
register: remnawave_sync_raw
changed_when: (remnawave_sync_raw.stdout | from_json).changed
no_log: true
- name: Разобрать результат синхронизации Remnawave
ansible.builtin.set_fact:
remnawave_sync: "{{ remnawave_sync_raw.stdout | from_json }}"
remnawave_xray_checker_url: "{{ (remnawave_sync_raw.stdout | from_json).xray_checker_subscription_url | default('') }}"
no_log: true
@@ -1,508 +0,0 @@
#!/bin/bash
set -euo pipefail
API_BASE="https://${RW_PANEL_DOMAIN}/api"
# Admin JWT без X-Remnawave-Client-Type: browser → 403 на всех эндпоинтах (Remnawave ≥3).
CURL=(curl -sSk --resolve "${RW_PANEL_DOMAIN}:443:127.0.0.1" -H "X-Remnawave-Client-Type: browser")
AUTH_HDR=()
admin_created=0
token_updated=0
profile_created=0
node_created=0
node_address_updated=0
host_created=0
host_remark_updated=0
node_secret=""
active_token="${RW_API_TOKEN:-}"
# В stderr/CI никогда не светить privateKey, SECRET_KEY, JWT, API-токены.
# publicKey оставляем — он не секрет.
redact_json() {
jq 'walk(
if type == "object" then
with_entries(
if (.key | test("(?i)privateKey|privKey|secretKey|SECRET_KEY|accessToken|apiToken|password|^token$|vlessUuid|trojanPassword|ssPassword|shortUuid|subscriptionUrl|setupKey|setup_key|METRICS_PASS"))
then .value = "[REDACTED]"
else .
end
)
else . end
)' 2>/dev/null || echo '[body redacted]'
}
log_api_error() {
local method="$1" path="$2" code="$3" body="$4"
echo "API ${method} ${path} → HTTP ${code}" >&2
printf '%s\n' "${body}" | redact_json >&2 || true
echo >&2
}
api() {
local method="$1"; shift
local path="$1"; shift
local tmp code
tmp=$(mktemp)
if [ -n "${AUTH_HDR[*]:-}" ]; then
code=$("${CURL[@]}" -o "$tmp" -w '%{http_code}' -X "$method" "${AUTH_HDR[@]}" \
-H "Content-Type: application/json" "${API_BASE}${path}" "$@") || true
else
code=$("${CURL[@]}" -o "$tmp" -w '%{http_code}' -X "$method" \
-H "Content-Type: application/json" "${API_BASE}${path}" "$@") || true
fi
if [[ ! "${code}" =~ ^2 ]]; then
log_api_error "$method" "$path" "$code" "$(cat "$tmp" 2>/dev/null || true)"
rm -f "$tmp"
return 1
fi
cat "$tmp"
rm -f "$tmp"
}
status_json=$("${CURL[@]}" "${API_BASE}/auth/status")
is_register=$(printf '%s' "${status_json}" | jq -r '.response.isRegisterAllowed // .isRegisterAllowed // false')
# Весь bootstrap держим на admin JWT + browser. API-токен создаём отдельно
# и НЕ переключаем на него: иначе пустые/неверные scopes → 403 на /keygen/.
if [ "${is_register}" = "true" ]; then
reg_payload=$(jq -n --arg u "$RW_ADMIN_USER" --arg p "$RW_ADMIN_PASS" '{username: $u, password: $p}')
reg_resp=$(api POST /auth/register -d "${reg_payload}")
access=$(printf '%s' "${reg_resp}" | jq -r '.response.accessToken // .accessToken // empty')
if [ -z "${access}" ]; then
echo "Не удалось зарегистрировать админа Remnawave" >&2
printf \'%s
\' "${reg_resp}" | redact_json >&2
exit 1
fi
AUTH_HDR=(-H "Authorization: Bearer ${access}")
admin_created=1
else
login_payload=$(jq -n --arg u "$RW_ADMIN_USER" --arg p "$RW_ADMIN_PASS" '{username: $u, password: $p}')
login_resp=$(api POST /auth/login -d "${login_payload}")
access=$(printf '%s' "${login_resp}" | jq -r '.response.accessToken // .accessToken // empty')
if [ -z "${access}" ]; then
echo "Не удалось войти в панель Remnawave (REMNAWAVE_ADMIN_* из /services/remnawave)" >&2
printf \'%s
\' "${login_resp}" | redact_json >&2
exit 1
fi
AUTH_HDR=(-H "Authorization: Bearer ${access}")
fi
# Каждый прогон перевыпускает именованный API-токен и гасит предыдущие
# (в том числе утёкший). Само значение наружу не печатаем.
tokens_json=$(api GET /tokens/ || echo '{"response":[]}')
old_uuids=$(printf '%s' "${tokens_json}" | jq -r --arg n "$RW_TOKEN_NAME" '
(.response // .)
| if type=="array" then . elif .tokens then .tokens else [] end
| map(select(.name == $n or .tokenName == $n) | .uuid)
| .[]')
for old_uuid in ${old_uuids}; do
[ -n "${old_uuid}" ] || continue
api DELETE "/tokens/${old_uuid}" >/dev/null || true
done
active_token=""
scopes_json=$(api GET /tokens/scopes || echo '{"response":{"wildcard":"*"}}')
wildcard=$(printf '%s' "${scopes_json}" | jq -r '.response.wildcard // "*"')
token_payload=$(jq -n --arg n "$RW_TOKEN_NAME" --arg s "${wildcard:-*}" \
'{name: $n, expiresInDays: 3650, scopes: [$s]}')
if token_resp=$(api POST /tokens/ -d "${token_payload}"); then
created=$(printf '%s' "${token_resp}" | jq -r '
.response.token // .response.apiToken // .token // .apiToken // empty')
if [ -n "${created}" ]; then
active_token="${created}"
token_updated=1
echo "API-токен ${RW_TOKEN_NAME} перевыпущен" >&2
else
echo "WARN: POST /tokens/ без поля token" >&2
fi
else
echo "WARN: не удалось перевыпустить API-токен" >&2
fi
if [ -z "${active_token}" ]; then
echo "Нет нового API-токена — bootstrap остановлен, секрет в лог не пишем" >&2
exit 1
fi
# SECRET_KEY ноды — отдельный endpoint /api/keygen/ (не ответ create node)
if [ -n "${RW_NODE_SECRET:-}" ]; then
node_secret="${RW_NODE_SECRET}"
else
keygen_resp=$(api GET /keygen/)
node_secret=$(printf '%s' "${keygen_resp}" | jq -r '.response.secretKey // .secretKey // empty')
if [ -z "${node_secret}" ]; then
echo "Не удалось получить SECRET_KEY ноды из /api/keygen/" >&2
printf \'%s
\' "${keygen_resp}" | redact_json >&2
exit 1
fi
fi
profiles_json=$(api GET /config-profiles/)
profile_uuid=$(printf '%s' "${profiles_json}" | jq -r --arg n "$RW_PROFILE_NAME" '
(.response.configProfiles // .response // [])
| map(select(.name == $n)) | .[0].uuid // empty')
pub=""
if [ -z "${profile_uuid}" ]; then
key_resp=$(api GET /system/tools/x25519/generate 2>/dev/null || echo '{}')
# v3: {response:{keypairs:[{publicKey,privateKey},...]}}
priv=$(printf '%s' "${key_resp}" | jq -r '
.response.keypairs[0].privateKey
// .response.privateKey // .response.privKey // .privateKey // .privKey // empty')
pub=$(printf '%s' "${key_resp}" | jq -r '
.response.keypairs[0].publicKey
// .response.publicKey // .response.pubKey // .publicKey // .pubKey // empty')
if [ -z "${priv}" ] || [ -z "${pub}" ]; then
echo "Не удалось сгенерировать X25519 ключи Reality" >&2
printf \'%s
\' "${key_resp}" | redact_json >&2
exit 1
fi
short_id=$(openssl rand -hex 8)
profile_config=$(sed \
-e "s|__REALITY_PRIVATE_KEY__|${priv}|g" \
-e "s|__REALITY_SHORT_ID__|${short_id}|g" \
"$RW_PROFILE_FILE")
create_profile=$(jq -n --arg n "$RW_PROFILE_NAME" --argjson c "${profile_config}" '{name: $n, config: $c}')
profile_resp=$(api POST /config-profiles/ -d "${create_profile}")
profile_uuid=$(printf '%s' "${profile_resp}" | jq -r '.response.uuid // .uuid // empty')
if [ -z "${profile_uuid}" ]; then
echo "Не удалось создать config profile" >&2
printf \'%s
\' "${profile_resp}" | redact_json >&2
exit 1
fi
profile_created=1
fi
profile_full=$(api GET "/config-profiles/${profile_uuid}")
inbound_uuid=$(printf '%s' "${profile_full}" | jq -r --arg t "$RW_INBOUND_TAG" '
(.response.inbounds // .inbounds // [])
| map(select(.tag == $t)) | .[0].uuid // empty')
if [ -z "${inbound_uuid}" ]; then
inbounds_json=$(api GET "/config-profiles/${profile_uuid}/inbounds" 2>/dev/null || echo '{}')
inbound_uuid=$(printf '%s' "${inbounds_json}" | jq -r --arg t "$RW_INBOUND_TAG" '
(.response // .)
| if type=="array" then . else (.inbounds // []) end
| map(select(.tag == $t)) | .[0].uuid // empty')
fi
if [ -z "${inbound_uuid}" ]; then
echo "Не найден inbound ${RW_INBOUND_TAG} в профиле ${profile_uuid}" >&2
exit 1
fi
nodes_json=$(api GET /nodes/)
node_uuid=$(printf '%s' "${nodes_json}" | jq -r --arg n "$RW_NODE_NAME" '
(.response // []) | map(select(.name == $n)) | .[0].uuid // empty')
if [ -z "${node_uuid}" ]; then
node_payload=$(jq -n \
--arg name "$RW_NODE_NAME" \
--arg addr "$RW_NODE_ADDR" \
--argjson port "$RW_NODE_PORT" \
--arg cc "$RW_NODE_CC" \
--arg pu "$profile_uuid" \
--arg iu "$inbound_uuid" \
'{
name: $name,
address: $addr,
port: $port,
countryCode: $cc,
configProfile: {
activeConfigProfileUuid: $pu,
activeInbounds: [$iu]
}
}')
node_resp=$(api POST /nodes/ -d "${node_payload}")
node_uuid=$(printf '%s' "${node_resp}" | jq -r '.response.uuid // .uuid // empty')
if [ -z "${node_uuid}" ]; then
echo "Не удалось создать ноду" >&2
printf \'%s
\' "${node_resp}" | redact_json >&2
exit 1
fi
node_created=1
fi
current_addr=$(printf '%s' "${nodes_json}" | jq -r --arg n "$RW_NODE_NAME" '(.response // []) | map(select(.name == $n)) | .[0].address // empty')
if [ -n "${RW_NODE_ADDR}" ] && [ "${current_addr}" != "${RW_NODE_ADDR}" ]; then
node_address_updated=1
fi
node_update=$(jq -n \
--arg u "$node_uuid" \
--arg pu "$profile_uuid" \
--arg iu "$inbound_uuid" \
--arg addr "$RW_NODE_ADDR" \
--argjson port "$RW_NODE_PORT" \
'{
uuid: $u,
address: $addr,
port: $port,
configProfile: {
activeConfigProfileUuid: $pu,
activeInbounds: [$iu]
}
}')
if ! api PATCH /nodes/ -d "${node_update}" >/dev/null \
&& ! api PUT /nodes/ -d "${node_update}" >/dev/null; then
echo "WARN: не удалось обновить адрес ноды ${RW_NODE_NAME}" >&2
fi
hosts_json=$(api GET /hosts/)
host_uuid=$(printf '%s' "${hosts_json}" | jq -r --arg a "$RW_SELFSTEAL" '
(.response // []) | map(select(.address == $a)) | .[0].uuid // empty')
if [ -z "${host_uuid}" ]; then
host_payload=$(jq -n \
--arg remark "$RW_HOST_REMARK" \
--arg addr "$RW_SELFSTEAL" \
--arg path "$RW_XHTTP_PATH" \
--arg sni "$RW_SELFSTEAL" \
--arg host "$RW_SELFSTEAL" \
--arg pu "$profile_uuid" \
--arg iu "$inbound_uuid" \
--arg nu "$node_uuid" \
'{
remark: $remark,
address: $addr,
port: 443,
path: $path,
sni: $sni,
host: $host,
fingerprint: "chrome",
isDisabled: false,
securityLayer: "DEFAULT",
alpn: "h2",
xHttpExtraParams: {
xmux: {
maxConcurrency: "16-32",
hMaxRequestTimes: "600-900",
hMaxReusableSecs: "1800-3000"
},
noSSEHeader: true,
xPaddingBytes: "100-1000"
},
inbound: {
configProfileUuid: $pu,
configProfileInboundUuid: $iu
},
nodes: [$nu]
}')
host_resp=$(api POST /hosts/ -d "${host_payload}")
host_uuid=$(printf '%s' "${host_resp}" | jq -r '.response.uuid // .uuid // empty')
if [ -z "${host_uuid}" ]; then
echo "Не удалось создать host" >&2
printf \'%s
\' "${host_resp}" | redact_json >&2
exit 1
fi
host_created=1
fi
if [ "${host_created}" -eq 0 ]; then
current_remark=$(printf '%s' "${hosts_json}" | jq -r --arg u "${host_uuid}" '
(.response // []) | map(select(.uuid == $u)) | .[0].remark // empty')
if [ "${current_remark}" != "${RW_HOST_REMARK}" ]; then
host_remark=$(jq -n --arg u "${host_uuid}" --arg remark "${RW_HOST_REMARK}" '{uuid: $u, remark: $remark}')
if ! api PATCH /hosts/ -d "${host_remark}" >/dev/null; then
echo "WARN: не удалось обновить имя хоста ${RW_SELFSTEAL}" >&2
else
host_remark_updated=1
echo "Имя хоста ${RW_SELFSTEAL}: ${RW_HOST_REMARK}" >&2
fi
fi
fi
users_changed=0
# Клиентский Xray JSON. Имя Except-RU: весь tcp/udp в proxy, RU/private/торренты и
# проверки IP в direct. Блок remnawave панель снимает до выдачи клиенту и
# подставляет этот хост как outbound с тегом proxy.
if [ -n "${RW_XRAY_TEMPLATE_FILE:-}" ] && [ -f "${RW_XRAY_TEMPLATE_FILE}" ]; then
want_tpl=$(jq -c . "${RW_XRAY_TEMPLATE_FILE}")
tpl_list=$(api GET /subscription-templates/)
tpl_uuid=$(printf '%s' "${tpl_list}" | jq -r --arg n "${RW_XRAY_TEMPLATE_NAME}" '
.response.templates[]? | select(.name == $n and .templateType == "XRAY_JSON") | .uuid' | head -1)
if [ -z "${tpl_uuid}" ]; then
tpl_body=$(jq -n --arg n "${RW_XRAY_TEMPLATE_NAME}" --argjson j "${want_tpl}" \
'{name: $n, templateType: "XRAY_JSON", templateJson: $j}')
tpl_resp=$(api POST /subscription-templates/ -d "${tpl_body}")
tpl_uuid=$(printf '%s' "${tpl_resp}" | jq -r '.response.uuid // empty')
if [ -z "${tpl_uuid}" ]; then
echo "Не удалось создать шаблон ${RW_XRAY_TEMPLATE_NAME}" >&2
exit 1
fi
echo "Шаблон ${RW_XRAY_TEMPLATE_NAME} создан" >&2
users_changed=1
else
tpl_full=$(api GET "/subscription-templates/${tpl_uuid}")
current_tpl=$(printf '%s' "${tpl_full}" | jq -cS '.response.templateJson // {}')
wanted_tpl=$(jq -cS . "${RW_XRAY_TEMPLATE_FILE}")
if [ "${current_tpl}" != "${wanted_tpl}" ]; then
upd=$(jq -n --arg u "${tpl_uuid}" --argjson j "${want_tpl}" '{uuid: $u, templateJson: $j}')
api PATCH /subscription-templates/ -d "${upd}" >/dev/null
echo "Шаблон ${RW_XRAY_TEMPLATE_NAME} обновлён" >&2
users_changed=1
else
echo "Шаблон ${RW_XRAY_TEMPLATE_NAME} уже совпадает" >&2
fi
fi
bound=$(printf '%s' "${hosts_json}" | jq -r --arg u "${host_uuid}" '.response[]? | select(.uuid == $u) | .xrayJsonTemplateUuid // empty')
if [ "${bound}" != "${tpl_uuid}" ]; then
host_bind=$(jq -n --arg u "${host_uuid}" --arg t "${tpl_uuid}" '{uuid: $u, xrayJsonTemplateUuid: $t}')
api PATCH /hosts/ -d "${host_bind}" >/dev/null
echo "Хост ${RW_SELFSTEAL} привязан к шаблону ${RW_XRAY_TEMPLATE_NAME}" >&2
users_changed=1
fi
fi
squad_uuid=$(api GET /internal-squads/ | jq -r --arg n "Default-Squad" '
.response.internalSquads[]? | select(.name == $n) | .uuid' | head -1)
if [ -n "${squad_uuid}" ] && [ -n "${inbound_uuid}" ]; then
has_inbound=$(api GET /internal-squads/ | jq -r --arg s "${squad_uuid}" --arg i "${inbound_uuid}" '
.response.internalSquads[]? | select(.uuid == $s) | .inbounds[]?.uuid | select(. == $i)' | head -1)
if [ -z "${has_inbound}" ]; then
squad_body=$(jq -n --arg u "${squad_uuid}" --arg i "${inbound_uuid}" '{uuid: $u, inbounds: [$i]}')
api PATCH /internal-squads/ -d "${squad_body}" >/dev/null
users_changed=1
fi
fi
# Конфиг страницы подписки. В списке поле config часто null — смотрим по имени.
sub_cfgs=$(api GET /subscription-page-configs/ || echo '{"response":{"configs":[]}}')
if ! printf '%s' "${sub_cfgs}" | jq -e '.response.configs[]? | select(.name == "Homelab")' >/dev/null; then
api POST /subscription-page-configs/ -d '{"name":"Homelab"}' >/dev/null
echo "Конфиг страницы подписки Homelab создан" >&2
users_changed=1
else
echo "Конфиг страницы подписки Homelab уже есть" >&2
fi
# Браузер: Accept и User-Agent без учёта регистра, правило BROWSER первым.
settings=$(api GET /subscription-settings/ || echo '')
if [ -n "${settings}" ]; then
patched=$(printf '%s' "${settings}" | jq '
.response
| .serveJsonAtBaseSubscription = true
| .responseRules.rules = (
[
{
name: "Browser",
enabled: true,
operator: "OR",
description: "IaC: HTML-страница подписки",
responseType: "BROWSER",
conditions: [
{headerName: "accept", operator: "CONTAINS", value: "text/html", caseSensitive: false},
{headerName: "user-agent", operator: "CONTAINS", value: "Mozilla", caseSensitive: false}
]
}
] + [
.responseRules.rules[]?
| select(.name != "Browser" and .name != "Browser Subscription" and .responseType != "BROWSER")
]
)')
api PATCH /subscription-settings/ -d "${patched}" >/dev/null \
|| api PUT /subscription-settings/ -d "${patched}" >/dev/null \
|| echo "WARN: не удалось обновить SRR" >&2
fi
if [ -n "${RW_USERS_FILE:-}" ] && [ -f "${RW_USERS_FILE}" ]; then
existing=$(api GET "/users/?size=500" || api GET /users/)
desired_names=$(jq -r '.users[].username' "${RW_USERS_FILE}")
while IFS= read -r username; do
[ -n "${username}" ] || continue
want=$(jq -c --arg u "${username}" '.users[] | select(.username == $u)' "${RW_USERS_FILE}")
squad_name=$(printf '%s' "${want}" | jq -r '.squad // "Default-Squad"')
sid=$(api GET /internal-squads/ | jq -r --arg n "${squad_name}" '.response.internalSquads[]? | select(.name == $n) | .uuid' | head -1)
uuid=$(printf '%s' "${existing}" | jq -r --arg u "${username}" '.response.users[]? | select(.username == $u) | .uuid' | head -1)
body=$(printf '%s' "${want}" | jq -c --arg sid "${sid}" '{
username,
status: (.status // "ACTIVE"),
expireAt: .expire_at,
description: (.description // ""),
trafficLimitBytes: (.traffic_limit_bytes // 0),
trafficLimitStrategy: (.traffic_limit_strategy // "NO_RESET"),
activeInternalSquads: (if $sid == "" then [] else [$sid] end)
}')
if [ -z "${uuid}" ]; then
api POST /users/ -d "${body}" >/dev/null
users_changed=1
else
api PATCH /users/ -d "$(printf '%s' "${body}" | jq -c --arg id "${uuid}" '. + {uuid: $id}')" >/dev/null
users_changed=1
fi
done <<< "${desired_names}"
while IFS= read -r row; do
[ -n "${row}" ] || continue
uname=${row%% *}
uuuid=${row#* }
if ! printf '%s\n' "${desired_names}" | grep -qx "${uname}"; then
api DELETE "/users/${uuuid}" >/dev/null
users_changed=1
fi
done < <(printf '%s' "${existing}" | jq -r '.response.users[]? | "\(.username)\t\(.uuid)"')
fi
xray_url=""
xray_user="${RW_XRAY_USER:-homelab}"
if [ -n "${xray_user}" ]; then
users_now=$(api GET "/users/?size=500" || api GET /users/ || echo '{}')
xray_url=$(printf '%s' "${users_now}" | jq -r --arg u "${xray_user}" '
.response.users[]? | select(.username == $u) | .subscriptionUrl // empty' | head -1)
if [ -z "${xray_url}" ]; then
short=$(printf '%s' "${users_now}" | jq -r --arg u "${xray_user}" '
.response.users[]? | select(.username == $u) | .shortUuid // empty' | head -1)
if [ -n "${short}" ] && [ -n "${RW_SUB_DOMAIN:-}" ]; then
xray_url="https://${RW_SUB_DOMAIN}/${short}"
fi
fi
fi
xray_saved=0
if [ -n "${xray_url}" ]; then
xray_saved=1
fi
# Секреты только в файл 0600. В stdout — статус без значений ключей.
secrets_file="${RW_SECRETS_FILE:-/opt/remnawave/.bootstrap-secrets.json}"
umask 077
jq -n \
--arg api_token "${active_token}" \
--arg node_secret "${node_secret}" \
--arg xray_url "${xray_url}" \
'{api_token: $api_token, node_secret: $node_secret, xray_checker_subscription_url: $xray_url}' > "${secrets_file}"
chmod 600 "${secrets_file}"
jq -n \
--arg profile_uuid "${profile_uuid}" \
--arg node_uuid "${node_uuid}" \
--arg host_uuid "${host_uuid}" \
--arg reality_pub "${pub}" \
--arg node_address "${RW_NODE_ADDR}" \
--argjson admin_created "${admin_created}" \
--argjson token_updated "${token_updated}" \
--argjson profile_created "${profile_created}" \
--argjson node_created "${node_created}" \
--argjson node_address_updated "${node_address_updated}" \
--argjson host_created "${host_created}" \
--argjson host_remark_updated "${host_remark_updated}" \
--argjson users_changed "${users_changed}" \
--argjson xray_saved "${xray_saved}" \
'{
profile_uuid: $profile_uuid,
node_uuid: $node_uuid,
host_uuid: $host_uuid,
node_address: $node_address,
reality_public_key: $reality_pub,
admin_created: ($admin_created == 1),
token_updated: ($token_updated == 1),
profile_created: ($profile_created == 1),
node_created: ($node_created == 1),
node_address_updated: ($node_address_updated == 1),
host_created: ($host_created == 1),
host_remark_updated: ($host_remark_updated == 1),
users_changed: ($users_changed == 1),
xray_checker_url_saved: ($xray_saved == 1),
changed: (($admin_created + $token_updated + $profile_created + $node_created + $node_address_updated + $host_created + $host_remark_updated + $users_changed + $xray_saved) > 0)
}'
@@ -1,9 +0,0 @@
---
- name: vmagent ноды
ansible.builtin.include_role:
name: vmagent
vars:
monitoring_node_exporter_public: false
monitoring_node_exporter_listen: "127.0.0.1:9100"
# Docker на этой машине не принимает API 1.41, которым говорит cAdvisor 0.49.
cadvisor_image: ghcr.io/google/cadvisor:v0.60.6
@@ -0,0 +1,5 @@
---
netbird_exit_group: "exit-{{ inventory_hostname }}"
netbird_exit_clients_group: "exit-clients"
netbird_exit_network_id: "exit-{{ inventory_hostname }}"
netbird_exit_route_description: "Exit Node {{ inventory_hostname }} (0.0.0.0/0)"
@@ -113,7 +113,7 @@
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 desc "${NB_EXIT_DESC}" \
--arg nid "${NB_EXIT_NET_ID}" \
--arg eg "${exit_group_id}" \
--arg cg "${exit_clients_group_id}" \
@@ -151,9 +151,10 @@
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"
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
File renamed without changes.

Before

Width:  |  Height:  |  Size: 234 B

After

Width:  |  Height:  |  Size: 234 B