224 lines
8.6 KiB
YAML
224 lines
8.6 KiB
YAML
---
|
|
- name: Проверить наличие обязательных секретов OPNsense и NetBird
|
|
ansible.builtin.assert:
|
|
that:
|
|
- opnsense_api_key is defined and opnsense_api_key | length > 0
|
|
- opnsense_api_secret is defined and opnsense_api_secret | length > 0
|
|
- netbird_setup_key is defined and netbird_setup_key | length > 0
|
|
- opnsense_netbird_management_url is defined and opnsense_netbird_management_url | length > 0
|
|
fail_msg: "Отсутствует один из обязательных секретов: OPNSENSE_API_KEY, OPNSENSE_API_SECRET или NETBIRD_SETUP_KEY."
|
|
|
|
- name: Проверить наличие плагина os-netbird в OPNsense
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/settings/get"
|
|
method: GET
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
status_code: [200, 404]
|
|
register: opnsense_nb_check
|
|
|
|
- name: Установить плагин os-netbird (если отсутствует)
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/core/firmware/install/os-netbird"
|
|
method: POST
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
body_format: json
|
|
body: {}
|
|
status_code: [200]
|
|
when: opnsense_nb_check.status == 404
|
|
|
|
- name: Дождаться появления API плагина os-netbird после установки
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/settings/get"
|
|
method: GET
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
status_code: [200]
|
|
register: opnsense_nb_settings_wait
|
|
retries: 30
|
|
delay: 5
|
|
until: opnsense_nb_settings_wait.status == 200
|
|
when: opnsense_nb_check.status == 404
|
|
|
|
- name: Сформировать текущие настройки плагина os-netbird
|
|
ansible.builtin.set_fact:
|
|
opnsense_nb_current_settings: "{{ (opnsense_nb_settings_wait.json if opnsense_nb_check.status == 404 else opnsense_nb_check.json).settings }}"
|
|
|
|
- name: Применить настройки сервиса NetBird в OPNsense
|
|
vars:
|
|
need_settings_update: >-
|
|
{{
|
|
(opnsense_nb_current_settings.general.enable | string) != '1' or
|
|
(opnsense_nb_current_settings.general.wireguardPort | string) != (opnsense_netbird_wg_port | string) or
|
|
(opnsense_nb_current_settings.firewall.allowConfig | string) != '1' or
|
|
(opnsense_nb_current_settings.firewall.blockInboundConnection | string) != '0' or
|
|
(opnsense_nb_current_settings.ssh.enable | string) != (opnsense_netbird_ssh_enable | string) or
|
|
(opnsense_nb_current_settings.dns.enable | string) != '1' or
|
|
(opnsense_nb_current_settings.routing.accessLan | string) != '1' or
|
|
(opnsense_nb_current_settings.routing.acceptClientRoutes | string) != '1' or
|
|
(opnsense_nb_current_settings.routing.acceptServerRoutes | string) != '1'
|
|
}}
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/settings/set"
|
|
method: POST
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
body_format: json
|
|
body:
|
|
settings:
|
|
general:
|
|
enable: "1"
|
|
wireguardPort: "{{ opnsense_netbird_wg_port }}"
|
|
firewall:
|
|
allowConfig: "1"
|
|
blockInboundConnection: "0"
|
|
ssh:
|
|
enable: "{{ opnsense_netbird_ssh_enable }}"
|
|
dns:
|
|
enable: "1"
|
|
routing:
|
|
accessLan: "1"
|
|
acceptClientRoutes: "1"
|
|
acceptServerRoutes: "1"
|
|
postquantum:
|
|
enableRosenpass: "0"
|
|
rosenpassPermissive: "0"
|
|
syslog:
|
|
logLevel: "info"
|
|
status_code: [200]
|
|
register: opnsense_nb_settings_set
|
|
when: need_settings_update
|
|
changed_when: true
|
|
|
|
- name: Переконфигурировать сервис NetBird в OPNsense
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/service/reconfigure"
|
|
method: POST
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
body_format: json
|
|
body: {}
|
|
status_code: [200]
|
|
when: opnsense_nb_settings_set is changed
|
|
changed_when: true
|
|
|
|
- name: Проверить статус службы NetBird в OPNsense
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/service/status"
|
|
method: GET
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
status_code: [200]
|
|
register: opnsense_nb_service_status
|
|
|
|
- name: Запустить службу NetBird в OPNsense (если остановлена)
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/service/start"
|
|
method: POST
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
body_format: json
|
|
body: {}
|
|
status_code: [200]
|
|
when: (opnsense_nb_service_status.json.status | default('')) != 'running'
|
|
changed_when: true
|
|
|
|
- name: Проверить текущее подключение NetBird в OPNsense
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/status/status"
|
|
method: GET
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
status_code: [200]
|
|
register: opnsense_nb_status
|
|
|
|
- name: Задать параметры аутентификации NetBird (Management URL и Setup Key)
|
|
vars:
|
|
nb_connected: "{{ opnsense_nb_status.json.management.connected | default(false) | bool }}"
|
|
nb_current_url: "{{ opnsense_nb_status.json.management.url | default('') }}"
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/authentication/set"
|
|
method: POST
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
body_format: json
|
|
body:
|
|
authentication:
|
|
managementUrl: "{{ opnsense_netbird_management_url }}"
|
|
setupKey: "{{ netbird_setup_key }}"
|
|
status_code: [200]
|
|
register: opnsense_nb_auth_set
|
|
when: (not nb_connected) or (nb_current_url != opnsense_netbird_management_url)
|
|
changed_when: true
|
|
no_log: true
|
|
|
|
- name: Подключить OPNsense к координатору NetBird (authentication/up)
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/authentication/up"
|
|
method: POST
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
body_format: json
|
|
body: {}
|
|
status_code: [200]
|
|
when: opnsense_nb_auth_set is changed
|
|
changed_when: true
|
|
|
|
- name: Синхронизировать конфигурацию клиента NetBird в OPNsense
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/settings/sync"
|
|
method: GET
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
status_code: [200]
|
|
when: opnsense_nb_settings_set is changed or opnsense_nb_auth_set is changed
|
|
changed_when: true
|
|
|
|
- name: Дождаться перехода пира OPNsense в состояние connected на новом сервере
|
|
ansible.builtin.uri:
|
|
url: "https://{{ opnsense_host }}/api/netbird/status/status"
|
|
method: GET
|
|
url_username: "{{ opnsense_api_key }}"
|
|
url_password: "{{ opnsense_api_secret }}"
|
|
force_basic_auth: true
|
|
validate_certs: false
|
|
status_code: [200]
|
|
register: opnsense_nb_final_status
|
|
retries: 15
|
|
delay: 4
|
|
until:
|
|
- (opnsense_nb_final_status.json.management.connected | default(false) | bool)
|
|
- (opnsense_nb_final_status.json.management.url | default('')) == opnsense_netbird_management_url
|
|
|
|
- name: Итог подключения OPNsense к NetBird
|
|
ansible.builtin.debug:
|
|
msg:
|
|
- "Management URL: {{ opnsense_nb_final_status.json.management.url }}"
|
|
- "Connected: {{ opnsense_nb_final_status.json.management.connected }}"
|
|
- "NetBird IP: {{ opnsense_nb_final_status.json.netbirdIp | default('N/A') }}"
|
|
- "FQDN: {{ opnsense_nb_final_status.json.fqdn | default('N/A') }}"
|
|
- "Networks: {{ opnsense_nb_final_status.json.networks | default([]) }}"
|