ansible automation devops
Ansible Inventory: Mehrere Server verwalten
Ein Server ist einfach. Aber wie skaliert man auf 10, 50 oder 100 Server?
Gruppen und Hierarchien
all:
children:
production:
children:
webservers:
hosts:
web-prod-1:
ansible_host: 10.0.1.10
web-prod-2:
ansible_host: 10.0.1.11
databases:
hosts:
db-prod-1:
ansible_host: 10.0.1.20
staging:
children:
webservers:
hosts:
web-stage-1:
ansible_host: 10.0.2.10
databases:
hosts:
db-stage-1:
ansible_host: 10.0.2.20
Jetzt kannst du:
# Alle Server
ansible all -m ping
# Nur Produktion
ansible production -m ping
# Nur Webserver (Prod + Staging)
ansible webservers -m ping
# Nur Produktion-Webserver
ansible 'production:&webservers' -m ping
Gruppen-Variablen
Statt Variablen pro Host, pro Gruppe definieren.
group_vars/all.yml - Für alle:
ansible_user: deploy
ansible_python_interpreter: /usr/bin/python3
timezone: Europe/Berlin
group_vars/production.yml - Nur Produktion:
env: production
log_level: warn
backup_enabled: true
group_vars/staging.yml - Nur Staging:
env: staging
log_level: debug
backup_enabled: false
group_vars/webservers.yml - Alle Webserver:
nginx_worker_processes: auto
nginx_worker_connections: 1024
Host-Variablen
Für einzelne Hosts:
host_vars/web-prod-1.yml:
nginx_worker_connections: 4096 # Überschreibt group_vars
primary_server: true
Variablen-Priorität
Von niedrig nach hoch:
group_vars/allgroup_vars/<gruppe>host_vars/<host>- Playbook vars
- Command line
-e
Verzeichnis-Struktur
ansible/
├── ansible.cfg
├── inventory/
│ ├── production.yml
│ └── staging.yml
├── group_vars/
│ ├── all.yml
│ ├── production.yml
│ ├── staging.yml
│ ├── webservers.yml
│ └── databases.yml
├── host_vars/
│ └── web-prod-1.yml
├── playbooks/
│ ├── update.yml
│ └── deploy.yml
└── roles/
└── ...
Mehrere Inventory-Dateien
# Ein Inventory
ansible-playbook -i inventory/production.yml playbooks/update.yml
# Mehrere kombinieren
ansible-playbook -i inventory/production.yml -i inventory/staging.yml playbooks/update.yml
Oder in ansible.cfg:
[defaults]
inventory = inventory/
Lädt alle YAML-Dateien im Verzeichnis.
Dynamische Inventories
Für Cloud-Anbieter: Server automatisch aus API holen.
Hetzner Cloud
pip install hcloud
hetzner.yml:
plugin: hcloud
token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
groups:
webservers: "'web' in name"
databases: "'db' in name"
export HCLOUD_TOKEN=dein-token
ansible-inventory -i hetzner.yml --list
AWS EC2
plugin: aws_ec2
regions:
- eu-central-1
filters:
instance-state-name: running
keyed_groups:
- key: tags.Role
prefix: role
Patterns für Host-Auswahl
# Alle
ansible all
# Eine Gruppe
ansible webservers
# Mehrere Gruppen (OR)
ansible 'webservers:databases'
# Intersection (AND)
ansible 'production:&webservers'
# Ausschluss (NOT)
ansible 'webservers:!web-prod-1'
# Regex
ansible '~web-.*'
# Limit
ansible all --limit web-prod-1
ansible all --limit 'web-*'
Praktisches Beispiel
Updates nur auf Prod-Webservern:
ansible-playbook -i inventory/ playbooks/update.yml --limit 'production:&webservers'
Rolling Update (2 Server gleichzeitig):
- name: Rolling update
hosts: webservers
serial: 2
tasks:
- name: Update
apt:
upgrade: dist
Tipps
- Konsistente Namenskonvention:
<rolle>-<env>-<nummer> - Gruppen logisch strukturieren: Nach Rolle UND Umgebung
- Variablen in group_vars: Nicht im Inventory selbst
- Dynamische Inventories für Cloud: Keine manuelle Pflege
Fazit
Gutes Inventory-Design spart später Stunden. Einmal richtig aufsetzen, dann skaliert Ansible von 5 auf 500 Server ohne Änderung.