81 lines
2.6 KiB
YAML
81 lines
2.6 KiB
YAML
---
|
|
- name: "GitOps Execution (Local Pull Mode)"
|
|
hosts: localhost
|
|
connection: local
|
|
gather_facts: true
|
|
become: true
|
|
|
|
vars:
|
|
# Pfade sind jetzt lokal auf dem Server
|
|
repo_root: "{{ playbook_dir }}/.."
|
|
apps_catalog_path: "{{ repo_root }}/apps"
|
|
deployments_path: "{{ repo_root }}/deployments"
|
|
base_deploy_path: "/opt"
|
|
|
|
# Vault Adresse für lokalen Zugriff
|
|
vault_addr: "https://10.100.30.11:8200"
|
|
|
|
tasks:
|
|
# 1. Identifikation
|
|
- name: "Bestimme Hostname (für Config Lookup)"
|
|
set_fact:
|
|
# ansible-pull läuft lokal, daher nehmen wir ansible_fqdn oder hostname
|
|
target_hostname: "{{ ansible_fqdn }}"
|
|
|
|
- name: "Suche Deployment-Definition"
|
|
stat:
|
|
path: "{{ deployments_path }}/{{ target_hostname }}.yml"
|
|
register: def_file
|
|
|
|
- name: "Abbruch wenn keine Config"
|
|
fail:
|
|
msg: "Keine Deployment-Config für {{ target_hostname }} gefunden."
|
|
when: not def_file.stat.exists
|
|
|
|
# 2. Lade Config (SOLL-Zustand)
|
|
- name: "Lade Host-Konfiguration"
|
|
include_vars:
|
|
file: "{{ deployments_path }}/{{ target_hostname }}.yml"
|
|
name: host_config
|
|
|
|
- name: "Definiere Soll-Apps"
|
|
set_fact:
|
|
wanted_apps: "{{ host_config.apps }}"
|
|
|
|
# 3. Ermittle IST-Zustand
|
|
- name: "Finde installierte Apps in {{ base_deploy_path }}"
|
|
find:
|
|
paths: "{{ base_deploy_path }}"
|
|
file_type: directory
|
|
recurse: false
|
|
register: installed_dirs
|
|
|
|
- name: "Filtere nicht-App Verzeichnisse (z.B. vault)"
|
|
set_fact:
|
|
# Wir nehmen an, dass alles in /opt eine App ist, außer explizite Ausnahmen
|
|
# Hier filtern wir nur Verzeichnisse, die Docker Compose Files haben könnten
|
|
installed_apps: "{{ installed_dirs.files | map(attribute='path') | map('basename') | list }}"
|
|
|
|
# 4. Bereinigung (Pruning)
|
|
- name: "Ermittle zu löschende Apps"
|
|
set_fact:
|
|
# Apps die installiert sind, aber nicht in wanted_apps stehen
|
|
# ACHTUNG: 'vault' sollte ggf. geschützt werden, wenn es manuell läuft?
|
|
# Da wir Vault aber auch via GitOps managen (in der Liste), ist das ok.
|
|
apps_to_remove: "{{ installed_apps | difference(wanted_apps) }}"
|
|
|
|
- name: "Pruning Loop"
|
|
include_tasks: prune_logic.yml
|
|
loop: "{{ apps_to_remove }}"
|
|
loop_control:
|
|
loop_var: app_name_to_remove
|
|
# Sicherheitshalber: Lösche nichts, was 'vault' heißt, falls Config kaputt ist
|
|
when: app_name_to_remove != 'vault'
|
|
|
|
# 5. Deploy Apps (Update/Install)
|
|
- name: "Deploy Apps Loop"
|
|
include_tasks: deploy_logic_pull.yml
|
|
loop: "{{ wanted_apps }}"
|
|
loop_control:
|
|
loop_var: app_name
|