feat(dns): rewrite add_service_route.yml to support multi-host Traefik configs
- Parse Host() rules from router definitions - Supports multiple hostnames per service file (e.g. semaphore + imagineering) - More robust and future-proof
This commit is contained in:
@@ -6,7 +6,8 @@
|
||||
#
|
||||
# 1. Syncs boilerplates/traefik/dynamic/ to lightning-lane
|
||||
# 2. Scans the directory for service configs
|
||||
# 3. Creates CNAME records for each service -> lightning-lane
|
||||
# 3. Extracts all hostnames from Host() rules (supports multi-host)
|
||||
# 4. Creates CNAME records for each hostname -> lightning-lane
|
||||
#
|
||||
# PREREQUISITES:
|
||||
# - Service dynamic config YAML committed to boilerplates/traefik/dynamic/
|
||||
@@ -14,15 +15,6 @@
|
||||
#
|
||||
# USAGE:
|
||||
# ansible-playbook -i inventory.yml playbooks/add_service_route.yml
|
||||
#
|
||||
# ADDING A NEW SERVICE:
|
||||
# 1. Create boilerplates/traefik/dynamic/<service>.yml
|
||||
# 2. Commit and push
|
||||
# 3. Run this playbook
|
||||
#
|
||||
# EXCLUDING FILES:
|
||||
# Files that are not service routes (e.g., default.yml for middleware
|
||||
# definitions) should be added to the exclude_configs list below.
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
- name: Sync Traefik routes and ensure DNS records
|
||||
@@ -39,7 +31,6 @@
|
||||
dynamic_config_dir: "{{ playbook_dir }}/../../boilerplates/traefik/dynamic"
|
||||
|
||||
# Files in the dynamic directory that are NOT service routes
|
||||
# (middleware definitions, TLS options, etc.)
|
||||
exclude_configs:
|
||||
- default.yml
|
||||
|
||||
@@ -53,38 +44,53 @@
|
||||
register: sync_result
|
||||
changed_when: "'sending incremental file list' in sync_result.stdout"
|
||||
|
||||
# ── Step 2: Discover service configs ──
|
||||
# ── Step 2: Discover hostnames from Traefik router rules ──
|
||||
- name: Find all dynamic config files
|
||||
ansible.builtin.find:
|
||||
paths: "{{ dynamic_config_dir }}"
|
||||
patterns: "*.yml"
|
||||
register: config_files
|
||||
|
||||
- name: Build service list from config filenames
|
||||
ansible.builtin.set_fact:
|
||||
service_names: >-
|
||||
{{ config_files.files
|
||||
| map(attribute='path')
|
||||
| map('basename')
|
||||
| reject('in', exclude_configs)
|
||||
| map('regex_replace', '\.yml$', '')
|
||||
| list }}
|
||||
- name: Read config files
|
||||
ansible.builtin.slurp:
|
||||
src: "{{ item.path }}"
|
||||
register: slurped_configs
|
||||
loop: "{{ config_files.files }}"
|
||||
when: item.path | basename not in exclude_configs
|
||||
|
||||
- name: Display services to route
|
||||
- name: Extract all hostnames from Host() rules
|
||||
ansible.builtin.set_fact:
|
||||
hostnames: >-
|
||||
{{
|
||||
slurped_configs.results
|
||||
| selectattr('content', 'defined')
|
||||
| map(attribute='content')
|
||||
| map('b64decode')
|
||||
| map('regex_findall', 'Host\\(`([^`]+)`\\)')
|
||||
| flatten
|
||||
| map('split', ' || ')
|
||||
| flatten
|
||||
| map('regex_replace', '`', '')
|
||||
| select('match', '.*\\.local\\.mk-labs\\.cloud$')
|
||||
| unique
|
||||
| list
|
||||
}}
|
||||
|
||||
- name: Display hostnames to create
|
||||
ansible.builtin.debug:
|
||||
msg: "Services found: {{ service_names }}"
|
||||
msg: "Hostnames found: {{ hostnames }}"
|
||||
|
||||
# ── Step 3: Create DNS CNAME records ──
|
||||
- name: Create DNS CNAME record for each service
|
||||
- name: Create DNS CNAME record for each hostname
|
||||
effectivelywild.technitium_dns.technitium_dns_add_record:
|
||||
api_url: "http://{{ dns_server }}.{{ base_domain }}"
|
||||
api_token: "{{ vault_technitium_api_key }}"
|
||||
zone: "{{ base_domain }}"
|
||||
name: "{{ item }}.{{ base_domain }}"
|
||||
name: "{{ item }}"
|
||||
type: "CNAME"
|
||||
cname: "lightning-lane.{{ base_domain }}"
|
||||
ttl: 360
|
||||
validate_certs: false
|
||||
loop: "{{ service_names }}"
|
||||
loop: "{{ hostnames }}"
|
||||
loop_control:
|
||||
label: "{{ item }}.{{ base_domain }}"
|
||||
label: "{{ item }}"
|
||||
|
||||
Reference in New Issue
Block a user