Ansible Setup & Gebruik
Vereisten
Op je lokale machine (waar je Ansible runt):
# Windows: gebruik WSL2
wsl --install
# In WSL/Linux:
sudo apt update
sudo apt install ansible python3-pip -y
# Installeer extra collections
ansible-galaxy collection install community.general ansible.posix
SSH Keys Configureren
Voordat Ansible werkt, moet je SSH key-based auth hebben:
# Genereer key (als je die nog niet hebt)
ssh-keygen -t ed25519 -C "homelab"
# Kopieer naar alle nodes
ssh-copy-id admin@192.0.2.201
ssh-copy-id admin@192.0.2.202
ssh-copy-id admin@192.0.2.203
# Test verbinding
ssh admin@192.0.2.201 "hostname"
Inventory
Setup:
cp ansible/inventory/hosts.yml.example ansible/inventory/hosts.yml
De nodes zijn gedefinieerd in ansible/inventory/hosts.yml:
all:
children:
k8s_cluster:
children:
control_plane:
hosts:
cp-01:
ansible_host: 192.0.2.201
workers:
hosts:
node-01:
ansible_host: 192.0.2.202
node-02:
ansible_host: 192.0.2.203
Playbooks
prepare-nodes.yml
Bereidt verse Ubuntu nodes voor op Kubernetes:
| Taak | Beschrijving |
|---|---|
| System updates | apt upgrade, autoremove |
| Intel microcode | CPU security patches |
| Essential packages | curl, git, htop, etc. |
| SSH hardening | Disable password auth, root login |
| UFW firewall | Allow SSH, K8s ports |
| Kernel modules | overlay, br_netfilter |
| Sysctl settings | IP forwarding, bridge netfilter |
| Disable swap | Vereist voor Kubernetes |
| Hostnames | Set hostname, update /etc/hosts |
Gebruik:
cd ansible
# Test eerst (dry-run)
ansible-playbook playbooks/prepare-nodes.yml --check
# Uitvoeren
ansible-playbook playbooks/prepare-nodes.yml
# Alleen op specifieke node
ansible-playbook playbooks/prepare-nodes.yml --limit cp-01
configure-passwordless-sudo.yml (eenmalig, daarna geen wachtwoord meer)
Zet passwordless sudo voor de Ansible-user (bijv. <user>) op alle nodes. Daarna hoeft geen enkel playbook meer --ask-become-pass.
cd ~/homelab/ansible
ansible-playbook playbooks/configure-passwordless-sudo.yml --ask-become-pass
Daarna alle andere playbooks zonder wachtwoord: ansible-playbook playbooks/...yml
deploy-ssh-keys.yml (<beheer-vm> toegang geven)
Als je Ansible vanaf de <beheer-vm> wilt draaien maar de <beheer-vm> nog geen SSH-toegang tot de nodes heeft: draai dit playbook eén keer vanaf het <werkstation> (waar je wél al SSH hebt). Het voegt de publieke sleutel toe op cp-01, node-01 en node-02 voor de user uit inventory (bijv. <user>).
# Op <werkstation> (in homelab/ansible; inventory moet voor <werkstation> kloppen: IP's, ansible_user, ansible_ssh_private_key_file):
ansible-playbook playbooks/deploy-ssh-keys.yml -e "ssh_public_key_file=~/.ssh/id_ed25519_homelab.pub" --ask-become-pass
# Of met key als string: -e "ssh_public_key_content='ssh-ed25519 AAAA... comment'"
Als <user> op de nodes sudo zonder wachtwoord heeft, kun je --ask-become-pass weglaten. Daarna kan de <beheer-vm> (met dezelfde key, bijv. ~/.ssh/id_ed25519_homelab) SSH-en naar de nodes en kun je de kubeadm-playbooks vanaf de <beheer-vm> draaien.
kubeadm-install-packages.yml en kubeadm-bootstrap.yml (migratie)
Voor de migratie Hard Way → kubeadm (zelfde hosts): installatie van kubeadm/kubelet/kubectl op alle nodes, daarna kubeadm init op de control plane en kubeadm join op de workers. Zie 30-migratie-kubeadm.md voor de volledige volgorde (Fase B handmatig, dan deze playbooks, daarna Fase F–L met kubectl/helm).
| Playbook | Doel |
|---|---|
playbooks/kubeadm-install-packages.yml |
Installeert kubeadm, kubelet, kubectl (apt; Debian/Ubuntu) |
playbooks/kubeadm-bootstrap.yml |
kubeadm init op cp-01, join op workers (idempotent) |
playbooks/kubeadm-post-bootstrap.yml |
Fase F–K: kubeconfig ophalen, Cilium, Gateway CRDs, MetalLB, cert-manager, Gateway-stack (draait op localhost/<beheer-vm>; vereist kubectl + helm op <beheer-vm>) |
Variabelen in group_vars/k8s_cluster.yml (versie, endpoint, pod/service CIDR). Optioneel voor post-bootstrap: -e "cert_manager_cloudflare_token=..." (anders secret handmatig aanmaken).
Handige Commands
# Ping alle nodes
ansible all -m ping
# Run ad-hoc command
ansible all -a "uptime"
# Check OS versie
ansible all -a "cat /etc/os-release"
# Reboot alle nodes
ansible all -m reboot
# Check playbook syntax
ansible-playbook playbooks/prepare-nodes.yml --syntax-check
Troubleshooting
"Permission denied (publickey)"
SSH key niet gekopieerd. Run ssh-copy-id eerst met wachtwoord auth.
"UNREACHABLE"
- Check of node aan staat
- Check IP adres in inventory
- Check firewall/netwerk
"Missing sudo password"
Inventory heeft ansible_become: true maar user kan geen passwordless sudo. Fix:
# Op de node:
echo "admin ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/admin
Volgende Stap
Na prepare-nodes.yml zijn de nodes klaar voor Kubernetes. Zie Kubernetes Setup.