Créer et valider la VM locale#

Le Vagrantfile choisit une box adaptée au système :

  • Windows : ubuntu/jammy64, version 20241002.0.0 ;
  • macOS : bento/ubuntu-24.04, version 202510.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/local

3. Vérifier VirtualBox#

Windows, même PowerShell#

& "$env:ProgramFiles\Oracle\VirtualBox\VBoxManage.exe" --version

macOS, même Terminal#

VBoxManage --version

Arrêtez-vous si la version ne s’affiche pas.

4. Vérifier Vagrant#

Terminal : PowerShell normal sous Windows, Terminal sous macOS
Dossier : infra/local
vagrant version

Arrêtez-vous si la version ne s’affiche pas.

5. Valider le Vagrantfile#

vagrant validate

Résultat attendu :

Vagrantfile validated successfully.

6. Créer la VM#

vagrant up --provider=virtualbox

Le premier lancement télécharge la box. Cela peut prendre plusieurs minutes.

7. Vérifier l’état Vagrant#

vagrant status

La 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-app

Si 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.txt

Affichez le fichier :

Get-Content .\vagrant-ssh-config.txt

macOS, même Terminal#

vagrant ssh-config > vagrant-ssh-config.txt

Affichez le fichier :

cat vagrant-ssh-config.txt

La 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-app

Windows ou macOS, même terminal#

bash scripts/prepare-local-ansible.sh

Ce 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 ansible

Activez l’environnement Python :

source ~/.venvs/cloud-com-devops/bin/activate

Chargez la configuration :

export ANSIBLE_CONFIG="$PWD/ansible.cfg"

Affichez l’inventaire :

ansible-inventory -i inventories/local.yml --graph

Testez la connexion :

ansible all -i inventories/local.yml -m ansible.builtin.ping

Résultat attendu :

voting_app | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Ne continuez pas tant que ce résultat n’apparaît pas.