Créer et valider la VM locale#
Le Vagrantfile choisit une box adaptée au système :
- Windows :
ubuntu/jammy64, version20241002.0.0; - macOS :
bento/ubuntu-24.04, version202510.26.0.
La VM utilise 2 processeurs, 3 Go de mémoire et l’adresse privée 192.168.56.20. Vagrant redirige aussi SSH vers 127.0.0.1 et un port local qu’il choisit.
1. Ouvrir VirtualBox une première fois#
Ouvrez VirtualBox, vérifiez que sa fenêtre s’affiche, puis fermez-la. Cette vérification détecte une installation ou un pilote incomplet.
2. Ouvrir le dossier local#
Windows, PowerShell normal#
cd "$HOME\Documents\tp-ansible-voting-app\infra\local"macOS, Terminal#
cd ~/Documents/tp-ansible-voting-app/infra/local3. Vérifier VirtualBox#
Windows, même PowerShell#
& "$env:ProgramFiles\Oracle\VirtualBox\VBoxManage.exe" --versionmacOS, même Terminal#
VBoxManage --versionArrêtez-vous si la version ne s’affiche pas.
4. Vérifier Vagrant#
Terminal : PowerShell normal sous Windows, Terminal sous macOS
Dossier : infra/localvagrant versionArrêtez-vous si la version ne s’affiche pas.
5. Valider le Vagrantfile#
vagrant validateRésultat attendu :
Vagrantfile validated successfully.6. Créer la VM#
vagrant up --provider=virtualboxLe premier lancement télécharge la box. Cela peut prendre plusieurs minutes.
7. Vérifier l’état Vagrant#
vagrant statusLa sortie doit contenir running (virtualbox). Ce statut ne suffit pas à valider la VM. Effectuez aussi le test SSH suivant.
8. Vérifier réellement le démarrage#
vagrant ssh -c "hostname"Le résultat attendu est :
voting-appSi aucun résultat n’apparaît après une minute, interrompez avec Ctrl+C et utilisez Aide et dépannage. Ne continuez pas vers Ansible.
9. Exporter la configuration SSH#
Windows, même PowerShell#
vagrant ssh-config | Set-Content -Encoding ascii .\vagrant-ssh-config.txtAffichez le fichier :
Get-Content .\vagrant-ssh-config.txtmacOS, même Terminal#
vagrant ssh-config > vagrant-ssh-config.txtAffichez le fichier :
cat vagrant-ssh-config.txtLa sortie doit contenir HostName, User, Port et IdentityFile.
10. Générer l’inventaire Ansible#
Windows, Ubuntu WSL#
Ouvrez le clone de Documents :
REPO_ROOT="$(
find /mnt/c/Users -maxdepth 4 -type d \
-path '*/Documents/tp-ansible-voting-app' -print -quit 2>/dev/null
)"
test -n "$REPO_ROOT" || {
echo "Dépôt introuvable dans Documents."
exit 1
}cd "$REPO_ROOT"macOS, Terminal#
cd ~/Documents/tp-ansible-voting-appWindows ou macOS, même terminal#
bash scripts/prepare-local-ansible.shCe script lit le port et la clé réellement choisis par Vagrant. Il crée ansible/inventories/local.yml.
11. Valider la connexion Ansible#
Ouvrez le dossier Ansible :
cd ansibleActivez l’environnement Python :
source ~/.venvs/cloud-com-devops/bin/activateChargez la configuration :
export ANSIBLE_CONFIG="$PWD/ansible.cfg"Affichez l’inventaire :
ansible-inventory -i inventories/local.yml --graphTestez la connexion :
ansible all -i inventories/local.yml -m ansible.builtin.pingRésultat attendu :
voting_app | SUCCESS => {
"changed": false,
"ping": "pong"
}Ne continuez pas tant que ce résultat n’apparaît pas.