From 8b8a122a5b79d6982c796c73b6461a8f93ab9197 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 9 Jan 2026 15:21:30 +0000 Subject: [PATCH] refined deplyoment strategy --- infrastructure/README.md | 17 +++++++++++++++-- infrastructure/ansible/deploy_logic_push.yml | 10 +--------- infrastructure/ansible/pull_deploy.yml | 10 +++++----- .../ansible/roles/common/tasks/gitops.yml | 8 ++++++++ .../common/templates/gitops-sync.service.j2 | 2 +- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/infrastructure/README.md b/infrastructure/README.md index a015c10..0910b16 100644 --- a/infrastructure/README.md +++ b/infrastructure/README.md @@ -78,8 +78,8 @@ Jetzt nutzen wir Ansible im **Push-Modus**, um Vault auf dem Zielserver zu insta ansible-galaxy install -r requirements.yml ``` -2. **Vault Deployen**: - Da Vault noch nicht läuft, wird Ansible Warnungen bei Secrets werfen (Permission Denied), aber das Deployment durchführen und den Container starten. +2. **Vault Deployen (Initial)**: + Da Vault noch nicht läuft, wird Ansible Warnungen bei Secrets werfen (Permission Denied/Connection Error), aber das Deployment durchführen und den Container starten. ```bash # Deploye auf alle Hosts im Inventory @@ -109,6 +109,19 @@ Jetzt nutzen wir Ansible im **Push-Modus**, um Vault auf dem Zielserver zu insta * Es fragt, ob `bootstrap.tfvars` gelöscht werden soll (Ja). * Es fragt, ob das **Root-Token** aus der Datei auf dem Server gelöscht werden soll (Ja, empfohlen für Security). +5. **Deployment abschließen (Apps & Secrets)**: + Jetzt, wo Vault gefüllt ist und wir ein Token haben, lassen wir Ansible die eigentlichen Apps (z.B. Traefik) erneut deployen – diesmal mit korrekten Secrets. + + ```bash + cd infrastructure/ansible + export VAULT_TOKEN='' + + # Wichtig: Token via Variable übergeben! + ansible-playbook -i inventory.ini deploy.yml -e "vault_token=$VAULT_TOKEN" + ``` + + ✅ **Ergebnis:** Alle Apps laufen und haben ihre korrekten `.env` Files. + --- ## Phase 3: Production Mode & GitOps diff --git a/infrastructure/ansible/deploy_logic_push.yml b/infrastructure/ansible/deploy_logic_push.yml index 91ef93b..ec923fe 100644 --- a/infrastructure/ansible/deploy_logic_push.yml +++ b/infrastructure/ansible/deploy_logic_push.yml @@ -27,17 +27,9 @@ # 3. Secrets aus Vault (Lokal lookup, Remote copy) # Nur ausführen, wenn has_secrets: true -- name: "Debug Vault Info" - debug: - msg: - - "Token vorhanden: {{ (vault_token | default(lookup('env', 'VAULT_TOKEN'))) | length > 0 }}" - - "Adresse: {{ lookup('env', 'VAULT_ADDR') }}" - delegate_to: localhost - when: app_item.has_secrets | default(false) - - name: "Lade Secrets aus Vault (Lokal lookup)" set_fact: - app_secrets: "{{ lookup('community.hashi_vault.vault_kv2_get', 'apps/' + app_item.name, engine_mount_point='secret', url=lookup('env', 'VAULT_ADDR') | default('https://10.100.30.11:8200'), token=(vault_token | default(lookup('env', 'VAULT_TOKEN'))), validate_certs=false) | default({}) }}" + app_secrets: "{{ lookup('community.hashi_vault.vault_kv2_get', 'apps/' + app_item.name, engine_mount_point='secret', url=lookup('env', 'VAULT_ADDR') | default('https://10.100.30.11:8200'), token=(vault_token | default(lookup('env', 'VAULT_TOKEN'))), validate_certs=true) | default({}) }}" delegate_to: localhost when: app_item.has_secrets | default(false) ignore_errors: true # Trotzdem ignorieren, falls Vault down ist oder Secret fehlt diff --git a/infrastructure/ansible/pull_deploy.yml b/infrastructure/ansible/pull_deploy.yml index 6e81943..7954ad8 100644 --- a/infrastructure/ansible/pull_deploy.yml +++ b/infrastructure/ansible/pull_deploy.yml @@ -50,11 +50,11 @@ recurse: false register: installed_dirs - - name: "Filtere nicht-App Verzeichnisse (z.B. vault)" + - name: "Filtere nicht-App Verzeichnisse (z.B. vault, stabify-infra)" 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 }}" + ignored_dirs: ['vault', 'stabify-infra', 'cni', 'containerd'] + installed_apps: "{{ installed_dirs.files | map(attribute='path') | map('basename') | difference(['vault', 'stabify-infra', 'cni', 'containerd']) | list }}" # 4. Bereinigung (Pruning) - name: "Ermittle zu löschende Apps" @@ -68,8 +68,8 @@ 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' + # Sicherheitshalber: Lösche nichts, was auf der Ignore-Liste steht (Redundant aber sicher) + when: app_name_to_remove not in ['vault', 'stabify-infra'] # 5. Rollen ausführen (Common & Users auch im Pull-Mode aktuell halten) - name: "Führe Rollen aus" diff --git a/infrastructure/ansible/roles/common/tasks/gitops.yml b/infrastructure/ansible/roles/common/tasks/gitops.yml index 89a935d..b90f294 100644 --- a/infrastructure/ansible/roles/common/tasks/gitops.yml +++ b/infrastructure/ansible/roles/common/tasks/gitops.yml @@ -1,3 +1,11 @@ +- name: "Initialer Checkout des GitOps Repositories" + git: + repo: "{{ git_repo_url }}" + dest: /opt/stabify-infra + version: main + force: true # Sicherstellen, dass es sauber ist + ignore_errors: true # Fallback, falls Repo noch leer/privat (wird von ansible-pull eh nochmal probiert) + - name: "Deploy GitOps Service Unit" template: src: gitops-sync.service.j2 diff --git a/infrastructure/ansible/roles/common/templates/gitops-sync.service.j2 b/infrastructure/ansible/roles/common/templates/gitops-sync.service.j2 index 24b1d77..361b451 100644 --- a/infrastructure/ansible/roles/common/templates/gitops-sync.service.j2 +++ b/infrastructure/ansible/roles/common/templates/gitops-sync.service.j2 @@ -12,7 +12,7 @@ User=root # -d: Checkout Verzeichnis # -i: Inventory (hier localhost) # pull_deploy.yml: Das Playbook im Repo -ExecStart=/usr/bin/ansible-pull -U {{ git_repo_url }} -d /opt/stabify-infra -i infrastructure/ansible/inventory_local.ini infrastructure/ansible/pull_deploy.yml +ExecStart=/usr/bin/ansible-pull -U {{ git_repo_url }} -d /opt/stabify-infra -i /opt/stabify-infra/infrastructure/ansible/inventory_local.ini infrastructure/ansible/pull_deploy.yml timeoutStartSec=600 [Install]