refactor: restructure repository into layered IaC architecture (ansible, terraform, kubernetes)

This commit is contained in:
karpenok committed 2026-11-08 18:36:52 +07:00
1 parent d5dfc02889
commit 3c300c0cde
29 files changed
+227 -1

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
host_authorized_keys:
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIF/yxhAs022l/8FlPlhyi7NeF3CvVGsev3B1HtKICO7M root_from_windows"
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEpwqB40k8QQq3k8160gfMKDaCJCX9qzvmuijfe3mO4I gitea-runner"
+6
View File
@@ -0,0 +1,6 @@
---
- name: Перезапустить SSH
ansible.builtin.service:
name: ssh
state: reloaded
+87
View File
@@ -0,0 +1,87 @@
---
- name: Подготовить Python для Ansible
ansible.builtin.raw: DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y python3 python3-apt
changed_when: false
- name: Собрать факты
ansible.builtin.setup:
- name: Создать каталог .ssh
ansible.builtin.file:
path: /root/.ssh
state: directory
owner: root
group: root
mode: "0700"
- name: Добавить авторизованные SSH-ключи
ansible.posix.authorized_key:
user: root
key: "{{ item }}"
state: present
loop: "{{ host_authorized_keys }}"
- name: Удалить cloud-init настройки пароля SSH
ansible.builtin.file:
path: /etc/ssh/sshd_config.d/50-cloud-init.conf
state: absent
notify: Перезапустить SSH
- name: Отключить вход по паролю в drop-in sshd_config.d
ansible.builtin.copy:
dest: /etc/ssh/sshd_config.d/00-disable-passwords.conf
content: |
PasswordAuthentication no
KbdInteractiveAuthentication no
owner: root
group: root
mode: "0644"
notify: Перезапустить SSH
- name: Удалить устаревший drop-in sshd_config.d
ansible.builtin.file:
path: /etc/ssh/sshd_config.d/99-disable-passwords.conf
state: absent
notify: Перезапустить SSH
- name: Запретить PasswordAuthentication в основном sshd_config
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: '^(#\s*)?PasswordAuthentication\s+.*'
line: 'PasswordAuthentication no'
notify: Перезапустить SSH
- name: Применить настройки SSH
ansible.builtin.meta: flush_handlers
- name: Поставить Docker
ansible.builtin.apt:
name:
- docker.io
- docker-compose-v2
- ufw
- ca-certificates
- curl
state: present
update_cache: true
- name: Включить Docker
ansible.builtin.systemd:
name: docker
enabled: true
state: started
- name: Разрешить SSH
ansible.builtin.command: ufw allow OpenSSH
register: ufw_ssh
changed_when: "'added' in (ufw_ssh.stdout | lower)"
- name: Проверить состояние firewall
ansible.builtin.command: ufw status
register: ufw_status
changed_when: false
- name: Включить firewall
ansible.builtin.command: ufw --force enable
when: "'Status: active' not in ufw_status.stdout"
changed_when: true
+8
View File
@@ -0,0 +1,8 @@
---
netbird_dir: /opt/netbird
netbird_ports:
- 80/tcp
- 443/tcp
- 3478/udp
netbird_letsencrypt_email: "{{ letsencrypt_email | default(admin_email) }}"
+6
View File
@@ -0,0 +1,6 @@
COMPOSE_PROJECT_NAME=netbird
NETBIRD_DOMAIN=
NETBIRD_LETSENCRYPT_EMAIL=
NETBIRD_AUTH_SECRET=
NETBIRD_DATASTORE_KEY=
NETBIRD_SESSION_KEY=
+152
View File
@@ -0,0 +1,152 @@
name: netbird
services:
traefik:
image: traefik:v3.7.14
container_name: netbird-traefik
restart: unless-stopped
networks:
netbird:
ipv4_address: 172.30.0.10
command:
- "--log.level=INFO"
- "--accesslog=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--providers.docker.network=netbird_netbird"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "--entrypoints.websecure.allowACMEByPass=true"
- "--entrypoints.web.http.aliasHeadersStrategy=delete"
- "--entrypoints.websecure.http.aliasHeadersStrategy=delete"
- "--entrypoints.websecure.transport.respondingTimeouts.readTimeout=0"
- "--entrypoints.websecure.transport.respondingTimeouts.writeTimeout=0"
- "--entrypoints.websecure.transport.respondingTimeouts.idleTimeout=0"
- "--entrypoints.web.http.redirections.entrypoint.to=websecure"
- "--entrypoints.web.http.redirections.entrypoint.scheme=https"
- "--certificatesresolvers.letsencrypt.acme.email=${NETBIRD_LETSENCRYPT_EMAIL}"
- "--certificatesresolvers.letsencrypt.acme.storage=/letsencrypt/acme.json"
- "--certificatesresolvers.letsencrypt.acme.tlschallenge=true"
- "--serverstransport.forwardingtimeouts.responseheadertimeout=0s"
- "--serverstransport.forwardingtimeouts.idleconntimeout=0s"
- "--ping=true"
- "--ping.entrypoint=ping"
- "--entrypoints.ping.address=:8082"
ports:
- "443:443"
- "80:80"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- netbird_traefik_letsencrypt:/letsencrypt
healthcheck:
test: ["CMD", "traefik", "healthcheck", "--ping", "--ping.entrypoint=ping", "--entrypoints.ping.address=:8082"]
interval: 5s
timeout: 3s
retries: 10
start_period: 10s
logging:
driver: json-file
options:
max-size: "500m"
max-file: "2"
dashboard:
image: netbirdio/dashboard@sha256:340b13c00b84349572a2acc5605e907cb09250a9e6461c757097eabdb8e02f2c
container_name: netbird-dashboard
restart: unless-stopped
networks: [netbird]
env_file:
- ./dashboard.env
depends_on:
server-health:
condition: service_healthy
labels:
- traefik.enable=true
- traefik.http.routers.netbird-dashboard.rule=Host(`${NETBIRD_DOMAIN}`)
- traefik.http.routers.netbird-dashboard.entrypoints=websecure
- traefik.http.routers.netbird-dashboard.tls=true
- traefik.http.routers.netbird-dashboard.tls.certresolver=letsencrypt
- traefik.http.routers.netbird-dashboard.service=dashboard
- traefik.http.routers.netbird-dashboard.priority=1
- traefik.http.services.dashboard.loadbalancer.server.port=80
logging:
driver: json-file
options:
max-size: "500m"
max-file: "2"
dashboard-health:
image: curlimages/curl:8.22.0
network_mode: service:dashboard
depends_on:
dashboard:
condition: service_started
entrypoint: ["sleep", "infinity"]
healthcheck:
test: ["CMD", "curl", "-fsS", "http://127.0.0.1/"]
interval: 5s
timeout: 3s
retries: 10
start_period: 15s
restart: unless-stopped
netbird-server:
image: netbirdio/netbird-server:0.82.0
container_name: netbird-server
restart: unless-stopped
networks: [netbird]
ports:
- "3478:3478/udp"
volumes:
- netbird_data:/var/lib/netbird
- ./config.yaml:/etc/netbird/config.yaml:ro
command: ["--config", "/etc/netbird/config.yaml"]
labels:
- traefik.enable=true
- traefik.http.routers.netbird-grpc.rule=Host(`${NETBIRD_DOMAIN}`) && (PathPrefix(`/signalexchange.SignalExchange/`) || PathPrefix(`/management.ManagementService/`) || PathPrefix(`/management.ProxyService/`))
- traefik.http.routers.netbird-grpc.entrypoints=websecure
- traefik.http.routers.netbird-grpc.tls=true
- traefik.http.routers.netbird-grpc.tls.certresolver=letsencrypt
- traefik.http.routers.netbird-grpc.service=netbird-server-h2c
- traefik.http.routers.netbird-grpc.priority=100
- traefik.http.routers.netbird-backend.rule=Host(`${NETBIRD_DOMAIN}`) && (PathPrefix(`/relay`) || PathPrefix(`/ws-proxy/`) || PathPrefix(`/api`) || PathPrefix(`/oauth2`))
- traefik.http.routers.netbird-backend.entrypoints=websecure
- traefik.http.routers.netbird-backend.tls=true
- traefik.http.routers.netbird-backend.tls.certresolver=letsencrypt
- traefik.http.routers.netbird-backend.service=netbird-server
- traefik.http.routers.netbird-backend.priority=100
- traefik.http.services.netbird-server.loadbalancer.server.port=80
- traefik.http.services.netbird-server-h2c.loadbalancer.server.port=80
- traefik.http.services.netbird-server-h2c.loadbalancer.server.scheme=h2c
logging:
driver: json-file
options:
max-size: "500m"
max-file: "2"
server-health:
image: curlimages/curl:8.22.0
network_mode: service:netbird-server
depends_on:
netbird-server:
condition: service_started
entrypoint: ["sleep", "infinity"]
healthcheck:
test: ["CMD", "curl", "-fsS", "http://127.0.0.1:80/oauth2/.well-known/openid-configuration"]
interval: 5s
timeout: 3s
retries: 12
start_period: 20s
restart: unless-stopped
volumes:
netbird_data:
netbird_traefik_letsencrypt:
networks:
netbird:
driver: bridge
ipam:
config:
- subnet: 172.30.0.0/24
gateway: 172.30.0.1
+8
View File
@@ -0,0 +1,8 @@
---
- name: Перезапустить NetBird
ansible.builtin.command: docker compose up -d --remove-orphans
args:
chdir: "{{ netbird_dir }}"
register: compose_up
changed_when: "'Started' in compose_up.stderr or 'Created' in compose_up.stderr or 'Recreated' in compose_up.stderr"
+164
View File
@@ -0,0 +1,164 @@
---
- name: Разрешить порты панели
ansible.builtin.command: ufw allow {{ item }}
register: ufw_rule
changed_when: "'added' in (ufw_rule.stdout | lower)"
loop: "{{ netbird_ports }}"
- name: Поставить openssl
ansible.builtin.apt:
name: openssl
state: present
- name: Создать каталог панели
ansible.builtin.file:
path: "{{ netbird_dir }}"
state: directory
owner: root
group: root
mode: "0700"
- name: Проверить, созданы ли уже ключи
ansible.builtin.stat:
path: "{{ netbird_dir }}/.env"
register: netbird_env
tags:
- config
- name: Проверить почту Let's Encrypt
ansible.builtin.assert:
that:
- netbird_letsencrypt_email is defined
- netbird_letsencrypt_email | length > 3
fail_msg: Укажите почту Let's Encrypt при запуске плейбука.
when: not netbird_env.stat.exists
- name: Сгенерировать ключи панели
ansible.builtin.shell: openssl rand -base64 32 | tr -d '\n'
register: netbird_generated_keys
changed_when: false
no_log: true
loop:
- auth
- datastore
- session
when: not netbird_env.stat.exists and netbird_auth_secret is not defined
- name: Запомнить новые ключи
ansible.builtin.set_fact:
netbird_auth_secret: "{{ netbird_generated_keys.results[0].stdout }}"
netbird_datastore_key: "{{ netbird_generated_keys.results[1].stdout }}"
netbird_session_key: "{{ netbird_generated_keys.results[2].stdout }}"
no_log: true
when: not netbird_env.stat.exists and netbird_auth_secret is not defined
- name: Прочитать сохранённые ключи
ansible.builtin.slurp:
src: "{{ netbird_dir }}/.env"
register: netbird_env_raw
no_log: true
when: netbird_env.stat.exists and netbird_auth_secret is not defined
tags:
- config
- name: Разобрать сохранённые ключи
ansible.builtin.set_fact:
netbird_auth_secret: "{{ env_map['NETBIRD_AUTH_SECRET'] }}"
netbird_datastore_key: "{{ env_map['NETBIRD_DATASTORE_KEY'] }}"
netbird_session_key: "{{ env_map['NETBIRD_SESSION_KEY'] }}"
netbird_letsencrypt_email: "{{ env_map['NETBIRD_LETSENCRYPT_EMAIL'] }}"
vars:
env_lines: "{{ (netbird_env_raw.content | b64decode).splitlines() | reject('equalto', '') | list }}"
env_map: "{{ dict(env_lines | map('split', '=', 1)) }}"
no_log: true
when: netbird_env.stat.exists and netbird_auth_secret is not defined
tags:
- config
- name: Записать .env
ansible.builtin.template:
src: env.j2
dest: "{{ netbird_dir }}/.env"
owner: root
group: root
mode: "0600"
no_log: true
notify: Перезапустить NetBird
tags:
- config
- name: Положить манифест панели
ansible.builtin.copy:
src: compose.yaml
dest: "{{ netbird_dir }}/compose.yaml"
owner: root
group: root
mode: "0644"
notify: Перезапустить NetBird
tags:
- config
- name: Собрать dashboard.env
ansible.builtin.template:
src: dashboard.env.j2
dest: "{{ netbird_dir }}/dashboard.env"
owner: root
group: root
mode: "0644"
notify: Перезапустить NetBird
tags:
- config
- name: Собрать config.yaml
ansible.builtin.template:
src: config.yaml.j2
dest: "{{ netbird_dir }}/config.yaml"
owner: root
group: root
mode: "0600"
no_log: true
notify: Перезапустить NetBird
tags:
- config
- name: Применить изменения конфигурации
ansible.builtin.meta: flush_handlers
tags:
- config
- name: Убедиться, что панель запущена
ansible.builtin.command: docker compose up -d
args:
chdir: "{{ netbird_dir }}"
register: compose_up
changed_when: "'Started' in compose_up.stderr or 'Created' in compose_up.stderr or 'Recreated' in compose_up.stderr"
tags:
- config
- name: Показать состояние контейнеров
ansible.builtin.command: docker compose ps
args:
chdir: "{{ netbird_dir }}"
register: compose_ps
changed_when: false
tags:
- config
- name: Дождаться ответа панели
ansible.builtin.command: >
curl -sk --resolve {{ netbird_domain }}:443:127.0.0.1
-o /dev/null -w '%{http_code}'
https://{{ netbird_domain }}/
register: panel_http
changed_when: false
retries: 30
delay: 10
until: panel_http.stdout in ['200', '302']
tags:
- config
- name: Итог установки
ansible.builtin.debug:
msg: "{{ compose_ps.stdout_lines + ['Первый вход: https://' + netbird_domain + '/setup'] }}"
tags:
- config
@@ -0,0 +1,32 @@
server:
listenAddress: ":80"
exposedAddress: "https://{{ netbird_domain }}:443"
stunPorts:
- 3478
metricsPort: 9090
healthcheckAddress: ":9000"
logLevel: "info"
logFile: "console"
authSecret: "{{ netbird_auth_secret }}"
dataDir: "/var/lib/netbird"
auth:
issuer: "https://{{ netbird_domain }}/oauth2"
signKeyRefreshEnabled: true
sessionCookieEncryptionKey: "{{ netbird_session_key }}"
dashboardRedirectURIs:
- "https://{{ netbird_domain }}/nb-auth"
- "https://{{ netbird_domain }}/nb-silent-auth"
cliRedirectURIs:
- "http://localhost:53000/"
reverseProxy:
trustedHTTPProxies:
- "172.30.0.10/32"
trustedPeers:
- "172.30.0.10/32"
store:
engine: "sqlite"
encryptionKey: "{{ netbird_datastore_key }}"
@@ -0,0 +1,12 @@
NETBIRD_MGMT_API_ENDPOINT=https://{{ netbird_domain }}
NETBIRD_MGMT_GRPC_API_ENDPOINT=https://{{ netbird_domain }}
AUTH_AUDIENCE=netbird-dashboard
AUTH_CLIENT_ID=netbird-dashboard
AUTH_CLIENT_SECRET=
AUTH_AUTHORITY=https://{{ netbird_domain }}/oauth2
USE_AUTH0=false
AUTH_SUPPORTED_SCOPES=openid profile email groups
AUTH_REDIRECT_URI=/nb-auth
AUTH_SILENT_REDIRECT_URI=/nb-silent-auth
NGINX_SSL_PORT=443
LETSENCRYPT_DOMAIN=none
+6
View File
@@ -0,0 +1,6 @@
COMPOSE_PROJECT_NAME=netbird
NETBIRD_DOMAIN={{ netbird_domain }}
NETBIRD_LETSENCRYPT_EMAIL={{ netbird_letsencrypt_email }}
NETBIRD_AUTH_SECRET={{ netbird_auth_secret }}
NETBIRD_DATASTORE_KEY={{ netbird_datastore_key }}
NETBIRD_SESSION_KEY={{ netbird_session_key }}