- Parse Host() rules from router definitions - Supports multiple hostnames per service file (e.g. semaphore + imagineering) - More robust and future-proof
97 lines
3.4 KiB
YAML
97 lines
3.4 KiB
YAML
---
|
|
# ------------------------------------------------------------------------------
|
|
# FILE: playbooks/add_service_route.yml
|
|
# DESCRIPTION: Ensures all services in the Traefik dynamic config directory
|
|
# are routed and have DNS CNAME records on monorail.
|
|
#
|
|
# 1. Syncs boilerplates/traefik/dynamic/ to lightning-lane
|
|
# 2. Scans the directory for service configs
|
|
# 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/
|
|
# - vault_technitium_api_key defined in group_vars/all/vault
|
|
#
|
|
# USAGE:
|
|
# ansible-playbook -i inventory.yml playbooks/add_service_route.yml
|
|
# ------------------------------------------------------------------------------
|
|
|
|
- name: Sync Traefik routes and ensure DNS records
|
|
hosts: localhost
|
|
connection: local
|
|
gather_facts: false
|
|
|
|
vars:
|
|
base_domain: "local.mk-labs.cloud"
|
|
dns_server: "monorail"
|
|
traefik_host: "10.1.71.35"
|
|
traefik_user: "wed"
|
|
traefik_dynamic_path: "/opt/docker/traefik/dynamic/"
|
|
dynamic_config_dir: "{{ playbook_dir }}/../../boilerplates/traefik/dynamic"
|
|
|
|
# Files in the dynamic directory that are NOT service routes
|
|
exclude_configs:
|
|
- default.yml
|
|
|
|
tasks:
|
|
# ── Step 1: Sync dynamic config to lightning-lane ──
|
|
- name: Sync Traefik dynamic configuration to lightning-lane
|
|
ansible.builtin.shell: >
|
|
rsync -av --delete
|
|
{{ dynamic_config_dir }}/
|
|
{{ traefik_user }}@{{ traefik_host }}:{{ traefik_dynamic_path }}
|
|
register: sync_result
|
|
changed_when: "'sending incremental file list' in sync_result.stdout"
|
|
|
|
# ── 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: 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: 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: "Hostnames found: {{ hostnames }}"
|
|
|
|
# ── Step 3: Create DNS CNAME records ──
|
|
- 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 }}"
|
|
type: "CNAME"
|
|
cname: "lightning-lane.{{ base_domain }}"
|
|
ttl: 360
|
|
validate_certs: false
|
|
loop: "{{ hostnames }}"
|
|
loop_control:
|
|
label: "{{ item }}"
|