99 lines
3.4 KiB
YAML
99 lines
3.4 KiB
YAML
---
|
|
- name: Bootstrap Homelab Environment
|
|
hosts: localhost
|
|
connection: local
|
|
gather_facts: false
|
|
|
|
vars:
|
|
libvirt_pool: "default"
|
|
libvirt_volumes:
|
|
- "k3s-server-disk.qcow2"
|
|
- "k3s-agent-1-disk.qcow2"
|
|
- "k3s-agent-2-disk.qcow2"
|
|
- "k3s-common-init.iso"
|
|
- "fedora-cloud-base.qcow2"
|
|
tofu_dir: "{{ playbook_dir }}/tofu"
|
|
ansible_dir: "{{ playbook_dir }}/ansible"
|
|
kubeconfig_path: "{{ playbook_dir }}/kubeconfig"
|
|
flux_git_url: "https://codeberg.org/Pata1704/homelab_gitops.git"
|
|
flux_git_branch: "main"
|
|
flux_git_path: "./clusters/production"
|
|
flux_git_token: "{{ lookup('env', 'GIT_TOKEN') }}"
|
|
|
|
tasks:
|
|
# --- 1. Libvirt Volumes prüfen und ggf. löschen ---
|
|
# Builtin-Only: Nicht möglich, daher als Hinweis ein Shell-Task:
|
|
- name: Remove old Libvirt volumes (Shell workaround, no builtin)
|
|
ansible.builtin.shell: |
|
|
for vol in {{ libvirt_volumes | join(' ') }}; do
|
|
if virsh vol-list {{ libvirt_pool }} | grep -q "$vol"; then
|
|
virsh vol-delete --pool {{ libvirt_pool }} "$vol"
|
|
fi
|
|
done
|
|
become: yes
|
|
changed_when: false # Setze auf true, wenn du das nachverfolgen willst
|
|
|
|
# --- 2. OpenTofu initialisieren und anwenden ---
|
|
# Builtin-Only: Nicht möglich, daher command-Module verwenden:
|
|
- name: Remove old OpenTofu state and lock files
|
|
ansible.builtin.file:
|
|
path: "{{ item }}"
|
|
state: absent
|
|
loop:
|
|
- "{{ tofu_dir }}/.terraform"
|
|
- "{{ tofu_dir }}/.terraform.lock.hcl"
|
|
- "{{ tofu_dir }}/terraform.tfstate"
|
|
- "{{ tofu_dir }}/terraform.tfstate.backup"
|
|
- "{{ tofu_dir }}/tofu.tfstate"
|
|
- "{{ tofu_dir }}/tofu.tfstate.backup"
|
|
|
|
- name: Initialize OpenTofu
|
|
ansible.builtin.command:
|
|
cmd: tofu init
|
|
chdir: "{{ tofu_dir }}"
|
|
changed_when: true
|
|
|
|
- name: Plan OpenTofu infrastructure
|
|
ansible.builtin.command:
|
|
cmd: tofu plan -out=tfplan
|
|
chdir: "{{ tofu_dir }}"
|
|
changed_when: false
|
|
|
|
- name: Apply OpenTofu infrastructure
|
|
ansible.builtin.command:
|
|
cmd: tofu apply -auto-approve
|
|
chdir: "{{ tofu_dir }}"
|
|
changed_when: true
|
|
|
|
# --- 3. K3s-Setup ---
|
|
- name: Run K3s Ansible Playbook
|
|
ansible.builtin.command:
|
|
cmd: 'ansible-playbook -i {{ ansible_dir }}/inventory.ini {{ ansible_dir }}/k3s_setup.yml -e ''ansible_ssh_common_args="-o StrictHostKeyChecking=accept-new"'' --ask-become-pass'
|
|
changed_when: true
|
|
|
|
# --- 4. Kubeconfig Hinweis ---
|
|
- name: Display KUBECONFIG info
|
|
ansible.builtin.debug:
|
|
msg:
|
|
- "K3s cluster setup should be complete."
|
|
- "To interact with your cluster, export the KUBECONFIG environment variable:"
|
|
- " export KUBECONFIG={{ kubeconfig_path }}"
|
|
- "Alternatively, copy '{{ kubeconfig_path }}' to '~/.kube/config' or merge it."
|
|
|
|
# --- 5. Flux Bootstrap ---
|
|
- name: Bootstrap Flux
|
|
ansible.builtin.command:
|
|
cmd: >
|
|
flux bootstrap git
|
|
--url={{ flux_git_url }}
|
|
--branch={{ flux_git_branch }}
|
|
--path={{ flux_git_path }}
|
|
--token-auth
|
|
environment:
|
|
KUBECONFIG: "{{ kubeconfig_path }}"
|
|
GIT_TOKEN: "{{ flux_git_token | default(lookup('env', 'GIT_TOKEN')) }}"
|
|
changed_when: true
|
|
|
|
- name: Final Bootstrap Message
|
|
ansible.builtin.debug:
|
|
msg: "Cluster-Bootstrap mit Ansible abgeschlossen!"
|