Contenu généré par IA
⚠️ Attention : Ce cheatsheet a été entièrement généré par IA. N’ayant, personnellement, jamais pratiqué Vagrant en production, les informations peuvent contenir des erreurs ou être incomplètes. Vérifiez toujours avec la documentation officielle avant utilisation !
Vagrant est un outil pour créer et gérer des environnements de développement virtualisés portables et reproductibles.
🚀 Installation
📌 Plateforme 🧠 Commande 🐧 Ubuntu/Debian wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor
🎩 CentOS/RHEL sudo yum install -y yum-utils && sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
🍺 macOS brew install vagrant
🪟 Windows choco install vagrant
📦 Binary direct Télécharger depuis vagrantup.com/downloads
Prérequis : VirtualBox, VMware ou Hyper-V installé
⚙️ Commandes essentielles
📌 Action 🧠 Commande 💡 Description 🏁 Initialiser vagrant init ubuntu/focal64
Crée un Vagrantfile avec une box ⬆️ Démarrer vagrant up
Lance la machine virtuelle 🔌 Connexion SSH vagrant ssh
Se connecter à la VM ⏸️ Suspendre vagrant suspend
Met en pause la VM 🔄 Redémarrer vagrant reload
Redémarre et recharge la config ⏹️ Arrêter vagrant halt
Arrête proprement la VM 💥 Détruire vagrant destroy
Supprime complètement la VM 📊 État vagrant status
Affiche l’état des VMs
📦 Gestion des boxes
📌 Action 🧠 Commande 📋 Lister boxes vagrant box list
⬇️ Ajouter box vagrant box add ubuntu/focal64
🗑️ Supprimer box vagrant box remove ubuntu/focal64
🔄 Mettre à jour vagrant box update
🧹 Nettoyer vagrant box prune
📝 Vagrantfile basique
Configuration simple
Vagrant . configure ( " 2 " ) do | config |
# Box de base
config . vm . box = " ubuntu/focal64 "
# Configuration 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 " . " , " /vagrant "
config . vm . synced_folder " ./src " , " /var/www "
# Configuration du provider
config . vm . provider " virtualbox " do | vb |
vb . name = " dev-server "
vb . memory = " 2048 "
vb . cpus = 2
vb . gui = false
end
# Provisioning
config . vm . provision " shell " , inline: <<-SHELL
apt-get update
apt-get install -y nginx
systemctl enable nginx
systemctl start nginx
SHELL
end
Multi-machines
Vagrant . configure ( " 2 " ) do | config |
# Serveur web
config . vm . define " web " do | web |
web . vm . box = " ubuntu/focal64 "
web . vm . network " private_network " , ip: " 192.168.33.10 "
web . vm . provider " virtualbox " do | vb |
vb . memory = " 1024 "
end
end
# Base de données
config . vm . define " db " do | db |
db . vm . box = " ubuntu/focal64 "
db . vm . network " private_network " , ip: " 192.168.33.11 "
db . vm . provider " virtualbox " do | vb |
vb . memory = " 2048 "
end
end
end
🌐 Configuration réseau
📌 Type 🧠 Configuration 💡 Usage 🔄 Port forwarding config.vm.network "forwarded_port", guest: 80, host: 8080
Accès depuis l’hôte 🏠 Réseau privé config.vm.network "private_network", ip: "192.168.33.10"
IP statique privée 🌍 Réseau public config.vm.network "public_network"
Accès depuis le réseau 🎯 DHCP privé config.vm.network "private_network", type: "dhcp"
IP auto privée
📁 Dossiers partagés
# Dossier simple
config . vm . synced_folder " . " , " /vagrant "
# Avec options
config . vm . synced_folder " ./data " , " /var/data " ,
owner: " www-data " ,
group: " www-data " ,
mount_options: [ " dmode=755 " , " fmode=644 " ]
# NFS (plus performant)
config . vm . synced_folder " . " , " /vagrant " ,
type: " nfs " ,
nfs_udp: false ,
nfs_version: 4
# Désactiver le dossier par défaut
config . vm . synced_folder " . " , " /vagrant " , disabled: true
🛠️ Provisioning
Shell inline
config . vm . provision " shell " , inline: <<-SHELL
apt-get update
apt-get install -y docker.io
usermod -aG docker vagrant
SHELL
Script externe
config . vm . provision " shell " , path: " bootstrap.sh "
config . vm . provision " shell " , path: " setup.sh " , args: [ " arg1 " , " arg2 " ]
Ansible
config . vm . provision " ansible " do | ansible |
ansible . playbook = " playbook.yml "
ansible . inventory_path = " inventory "
ansible . limit = " all "
end
Docker
config . vm . provision " docker " do | docker |
docker . pull_images " nginx:latest "
docker . pull_images " mysql:5.7 "
docker . run " nginx " ,
args: " -p 80:80 -v /vagrant:/usr/share/nginx/html "
end
🔧 Providers
VirtualBox
config . vm . provider " virtualbox " do | vb |
vb . name = " my-vm "
vb . memory = " 2048 "
vb . cpus = 2
vb . gui = false
vb . customize [ " modifyvm " , :id , " --natdnshostresolver1 " , " on " ]
vb . customize [ " modifyvm " , :id , " --natdnsproxy1 " , " on " ]
end
VMware
config . vm . provider " vmware_desktop " do | vmware |
vmware . vmx [ " memsize " ] = " 2048 "
vmware . vmx [ " numvcpus " ] = " 2 "
vmware . vmx [ " displayName " ] = " My VM "
end
Hyper-V
config . vm . provider " hyperv " do | hv |
hv . memory = 2048
hv . cpus = 2
hv . vmname = " my-vm "
end
🔍 Debugging & Troubleshooting
📌 Debug 🧠 Commande 🐛 Mode debug VAGRANT_LOG=debug vagrant up
📋 Global status vagrant global-status
🧹 Nettoyer cache vagrant global-status --prune
🔧 Recharger config vagrant reload --provision
📊 Info SSH vagrant ssh-config
📦 Boxes populaires
📌 Box 💡 Description ubuntu/focal64
Ubuntu 20.04 LTS ubuntu/jammy64
Ubuntu 22.04 LTS centos/7
CentOS 7 centos/stream8
CentOS Stream 8 debian/bullseye64
Debian 11 generic/fedora36
Fedora 36 bento/ubuntu-20.04
Ubuntu 20.04 (Chef Bento)
⚡ Plugins utiles
📌 Plugin 🧠 Installation 💡 Usage vagrant-hostsupdater vagrant plugin install vagrant-hostsupdater
MAJ auto /etc/hosts vagrant-vbguest vagrant plugin install vagrant-vbguest
MAJ auto VBoxGuestAdditions vagrant-disksize vagrant plugin install vagrant-disksize
Redimensionner le disque
🎯 Bonnes pratiques
📌 Pratique 💡 Description 📁 Organisation Un Vagrantfile par projet 🧹 Nettoyage Détruire les VMs inutilisées 🚀 Performance Utiliser NFS pour dossiers partagés 📦 Boxes Utiliser des boxes officielles