Infrastructure as Code per il homelab
Ansible permette di configurare e gestire decine di server da un unico punto di controllo, con playbook dichiarativi in YAML.
1. Installazione (solo sul controller)
apt install ansible -y
# Non serve installare nulla sui target (usa SSH)
2. Inventario (/etc/ansible/hosts)
[proxmox]
server3 ansible_host=192.168.1.81
pve10 ansible_host=192.168.1.90
pve-ai ansible_host=192.168.1.108
[webservers]
web01 ansible_host=192.168.1.92
[all:vars]
ansible_user=root
3. Comandi ad-hoc
# Ping tutti i server
ansible all -m ping
# Aggiornare tutti
ansible all -m apt -a "upgrade=dist update_cache=yes"
# Verificare uptime
ansible all -m command -a "uptime"
4. Playbook
# setup-webserver.yml
---
- hosts: webservers
tasks:
- name: Installare Apache
apt:
name: [apache2, php, php-mysql, libapache2-mod-php]
state: present
update_cache: yes
- name: Abilitare Apache
service:
name: apache2
state: started
enabled: yes
- name: Copiare configurazione
copy:
src: files/apache.conf
dest: /etc/apache2/sites-available/000-default.conf
notify: Restart Apache
handlers:
- name: Restart Apache
service:
name: apache2
state: restarted
ansible-playbook setup-webserver.yml
5. Roles (organizzazione modulare)
ansible-galaxy init roles/webserver
# Struttura: tasks/, handlers/, templates/, files/, vars/
Ansible elimina la configurazione manuale ripetitiva: un playbook testato una volta funziona su 1 o 100 server identicamente.