Cheat Sheet
Commandes de base
| Commande | Description |
vagrant init | Initialise un Vagrantfile dans le répertoire courant |
vagrant up | Démarre et provisionne la machine virtuelle |
vagrant halt | Arrête la machine virtuelle |
vagrant reload | Redémarre la machine (utile après modif du Vagrantfile) |
vagrant destroy | Supprime la machine virtuelle |
vagrant status | Affiche l'état de la machine |
vagrant suspend | Met en veille la machine |
vagrant resume | Réveille la machine suspendue |
vagrant ssh | Se connecte en SSH à la machine |
vagrant ssh-config | Affiche la configuration SSH |
Gestion des boxes
| Commande | Description |
vagrant box list | Liste les boxes installées |
vagrant box add <nom> | Ajoute une box |
vagrant box remove <nom> | Supprime une box |
vagrant box update | Met à jour une box |
vagrant box outdated | Vérifie les mises à jour disponibles |
Provisioning
| Commande | Description |
vagrant provision | Exécute le provisionneur |
vagrant up --provision | Démarre et force le provisionnement |
vagrant reload --provision | Redémarre et provisionne |
Snapshots
| Commande | Description |
vagrant snapshot save <nom> | Crée un snapshot |
vagrant snapshot list | Liste les snapshots |
vagrant snapshot restore <nom> | Restaure un snapshot |
vagrant snapshot delete <nom> | Supprime un snapshot |
vagrant snapshot push | Crée un snapshot automatiquement |
vagrant snapshot pop | Restaure le dernier snapshot |
Configuration Vagrantfile basique
rubyVagrant.configure("2") do |config|
# Box de base
config.vm.box = "ubuntu/focal64"
# Réseau
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "forwarded_port", guest: 80, host: 8080
# Dossiers partagés
config.vm.synced_folder "./data", "/var/www/html"
# Provider spécifique (VirtualBox)
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.cpus = 2
vb.name = "ma-vm"
end
# Provisioning shell
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
SHELL
end
Providers courants
| Provider | Configuration |
| VirtualBox | config.vm.provider "virtualbox" |
| VMware | config.vm.provider "vmware_desktop" |
| Docker | config.vm.provider "docker" |
| Hyper-V | config.vm.provider "hyperv" |
| AWS | config.vm.provider "aws" |
Réseau
| Type | Description | Exemple |
| Port forwarding | Redirection de ports | config.vm.network "forwarded_port", guest: 80, host: 8080 |
| Private network | Réseau privé | config.vm.network "private_network", ip: "192.168.33.10" |
| Public network | Bridge réseau | config.vm.network "public_network" |
Boxes populaires ici
| Box | Description |
ubuntu/focal64 | Ubuntu 20.04 LTS |
ubuntu/jammy64 | Ubuntu 22.04 LTS |
centos/7 | CentOS 7 |
centos/8 | CentOS 8 |
debian/buster64 | Debian 10 |
debian/bullseye64 | Debian 11 |
hashicorp/bionic64 | Ubuntu 18.04 (minimal) |
Plugins utiles
| Plugin | Commande d'installation | Description |
| vagrant-hostmanager | vagrant plugin install vagrant-hostmanager | Gestion automatique du fichier hosts |
| vagrant-vbguest | vagrant plugin install vagrant-vbguest | Maintient à jour les additions invités |
| vagrant-disksize | vagrant plugin install vagrant-disksize | Permet de redimensionner le disque |
| vagrant-cachier | vagrant plugin install vagrant-cachier | Cache les paquets pour accélérer |
Variables d'environnement utiles
| Variable | Description |
VAGRANT_VAGRANTFILE | Nom du fichier Vagrantfile |
VAGRANT_CWD | Répertoire de travail |
VAGRANT_HOME | Répertoire global de Vagrant |
VAGRANT_LOG | Niveau de log (debug, info, warn, error) |
Dépannage
bash# Vérifier la configuration
vagrant validate
# Afficher les logs détaillés
VAGRANT_LOG=debug vagrant up
# Forcer la destruction
vagrant destroy -f
# Recharger complètement
vagrant destroy -f && vagrant up
Structure typique d'un projet
mon-projet/
├── Vagrantfile
├── data/ # Dossier partagé
├── scripts/ # Scripts de provisioning
│ └── setup.sh
├── configs/ # Fichiers de configuration
└── README.md
Astuces 💡
- Utilisez vagrant reload après modification du réseau ou des dossiers partagés
- Les snapshots sont parfaits pour tester des configurations risquées
- Pensez à versionner votre Vagrantfile mais pas le dossier .vagrant/
- Utilisez des boucles dans le Vagrantfile pour créer plusieurs machines
ruby# Exemple multi-machines
Vagrant.configure("2") do |config|
config.vm.define "web" do |web|
web.vm.box = "ubuntu/focal64"
web.vm.network "private_network", ip: "192.168.33.10"
end
config.vm.define "db" do |db|
db.vm.box = "ubuntu/focal64"
db.vm.network "private_network", ip: "192.168.33.11"
end
end
- Doc Vagrant sous Archlinux