Zum Inhalt

Vagrant

Umfassende HashiCorp Vagrant Befehle und Workflows für Entwicklungsumgebungsmanagement und virtuelle Maschinenbereitstellung.

Installation & Einrichtung

Befehl Beschreibung
vagrant version Zeige Vagrant-Version
vagrant -h Hilfe-Informationen anzeigen
vagrant init Neue Vagrantfile initialisieren
vagrant init ubuntu/jammy64 Mit spezifischer Box initialisieren
## Box-Verwaltung

Box-Operationen

Befehl Beschreibung
vagrant box list Installierte Boxen auflisten
vagrant box add ubuntu/jammy64 Box hinzufügen
vagrant box add --name mybox /path/to/box.box Lokale Box hinzufügen
vagrant box remove ubuntu/jammy64 Box entfernen
vagrant box update Alle Boxen aktualisieren
vagrant box update --box ubuntu/jammy64 Bestimmte Box aktualisieren
### Box-Informationen
Befehl Beschreibung
--------- -------------
vagrant box outdated Auf veraltete Boxen prüfen
vagrant box prune Alte Box-Versionen entfernen
vagrant box repackage ubuntu/jammy64 Box neu verpacken
## VM-Lebenszyklus-Management

Grundlegende Operationen

Befehl Beschreibung
vagrant up VM starten und bereitstellen
vagrant halt Shutdown VM
vagrant reload VM neu starten
vagrant destroy VM zerstören
vagrant suspend VM aussetzen
vagrant resume Gestoppte VM fortsetzen
### VM-Status und Informationen
Befehl Beschreibung
--------- -------------
vagrant status VM-Status anzeigen
vagrant global-status Status aller VMs anzeigen
vagrant global-status --prune Ungültige Einträge bereinigen
## SSH und Zugriff

SSH-Operationen

Befehl Beschreibung
vagrant ssh SSH in die VM
vagrant ssh-config SSH-Konfiguration anzeigen
vagrant ssh -- -L 8080:localhost:80 SSH mit Port-Weiterleitung
## Bereitstellung

Bereitstellungsbefehle

Befehl Beschreibung
vagrant provision Provisioner ausführen
vagrant provision --provision-with shell Bestimmten Provisioner ausführen
vagrant up --provision Starten und bereitstellen
vagrant reload --provision Neu starten und bereitstellen
## Multi-Maschinen-Umgebungen

Multi-Maschinen-Befehle

Befehl Beschreibung
vagrant up web Starte spezifische Maschine
vagrant ssh web SSH zu bestimmter Maschine
vagrant halt db Stopp spezifische Maschine
vagrant destroy --force Zerstöre alle ohne Bestätigung
## Snapshots

Snapshot-Verwaltung

Befehl Beschreibung
vagrant snapshot save snapshot_name Schnappschuss speichern
vagrant snapshot list Snapshots auflisten
vagrant snapshot restore snapshot_name Snapshot wiederherstellen
vagrant snapshot delete snapshot_name Snapshot löschen
## Plugin-Verwaltung

Plugin-Operationen

Befehl Beschreibung
vagrant plugin list Installierte Plugins auflisten
vagrant plugin install vagrant-vbguest Plugin installieren
vagrant plugin uninstall vagrant-vbguest Plugin deinstallieren
vagrant plugin update Alle Plugins aktualisieren
## Vagrantfile-Beispiele

Basis-Vagrantfile

Vagrant.configure("2") do|config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.hostname = "myserver"

  # Network configuration
  config.vm.network "private_network", ip: "192.168.56.10"
  config.vm.network "forwarded_port", guest: 80, host: 8080

  # Provider configuration
  config.vm.provider "virtualbox" do|vb|
    vb.memory = "2048"
    vb.cpus = 2
    vb.name = "MyVM"
  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-Maschinen-Setup

Vagrant.configure("2") do|config|
  # Web server
  config.vm.define "web" do|web|
    web.vm.box = "ubuntu/jammy64"
    web.vm.hostname = "web"
    web.vm.network "private_network", ip: "192.168.56.10"

    web.vm.provider "virtualbox" do|vb|
      vb.memory = "1024"
      vb.cpus = 1
    end

    web.vm.provision "shell", inline: <<-SHELL
      apt-get update
      apt-get install -y nginx
    SHELL
  end

  # Database server
  config.vm.define "db" do|db|
    db.vm.box = "ubuntu/jammy64"
    db.vm.hostname = "database"
    db.vm.network "private_network", ip: "192.168.56.11"

    db.vm.provider "virtualbox" do|vb|
      vb.memory = "2048"
      vb.cpus = 2
    end

    db.vm.provision "shell", inline: <<-SHELL
      apt-get update
      apt-get install -y mysql-server
    SHELL
  end
end

Erweiterte Konfiguration

Vagrant.configure("2") do|config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.box_version = "20230607.0.0"

  # Synced folders
  config.vm.synced_folder ".", "/vagrant", disabled: true
  config.vm.synced_folder "./app", "/var/www/html",
    owner: "www-data", group: "www-data"

  # Network configuration
  config.vm.network "private_network",
    ip: "192.168.56.10",
    netmask: "255.255.255.0"

  config.vm.network "public_network",
    bridge: "en0: Wi-Fi (AirPort)"

  # Port forwarding
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "forwarded_port", guest: 443, host: 8443

  # Provider-specific configuration
  config.vm.provider "virtualbox" do|vb|
    vb.gui = false
    vb.memory = "4096"
    vb.cpus = 4
    vb.name = "Development Server"

    # VirtualBox specific settings
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
  end

  # Multiple provisioners
  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get upgrade -y
  SHELL

  config.vm.provision "file",
    source: "./configs/nginx.conf",
    destination: "/tmp/nginx.conf"

  config.vm.provision "shell", path: "scripts/setup.sh"

  config.vm.provision "ansible" do|ansible|
    ansible.playbook = "playbook.yml"
    ansible.inventory_path = "inventory"
  end
end

Bereitstellungsmethoden

Shell-Bereitstellung

# Inline shell commands
config.vm.provision "shell", inline: <<-SHELL
  apt-get update
  apt-get install -y docker.io
  usermod -aG docker vagrant
SHELL

# External script
config.vm.provision "shell", path: "scripts/bootstrap.sh"

# Privileged and non-privileged
config.vm.provision "shell", privileged: false, inline: <<-SHELL
  echo "Running as vagrant user"
SHELL

# Environment variables
config.vm.provision "shell", env: \\\\{"DEBIAN_FRONTEND" => "noninteractive"\\\\}, inline: <<-SHELL
  apt-get install -y mysql-server
SHELL

Datei-Bereitstellung

config.vm.provision "file",
  source: "~/.ssh/id_rsa.pub",
  destination: "~/.ssh/authorized_keys"

config.vm.provision "file",
  source: "./configs/",
  destination: "/tmp/configs"

Ansible-Bereitstellung

config.vm.provision "ansible" do|ansible|
  ansible.playbook = "site.yml"
  ansible.inventory_path = "inventory/vagrant"
  ansible.limit = "all"
  ansible.verbose = "v"

  ansible.extra_vars = \\\\{
    nginx_port: 80,
    database_name: "myapp"
  \\\\}
end

Docker-Bereitstellung

config.vm.provision "docker" do|d|
  d.pull_images "nginx"
  d.pull_images "mysql:8.0"

  d.run "nginx",
    args: "-p 80:80 -v /vagrant/html:/usr/share/nginx/html"

  d.run "mysql",
    args: "-e MYSQL_ROOT_PASSWORD=secret -p 3306:3306"
end

Netzwerkkonfiguration

Private Netzwerke

# Static IP
config.vm.network "private_network", ip: "192.168.56.10"

# DHCP
config.vm.network "private_network", type: "dhcp"

# Multiple interfaces
config.vm.network "private_network", ip: "192.168.56.10"
config.vm.network "private_network", ip: "10.0.0.10"

Öffentliche Netzwerke

Would you like me to continue with the remaining translations or provide more details for the empty sections?```ruby

Bridge to default interface

config.vm.network "public_network"

Bridge to specific interface

config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"

Static IP on public network

config.vm.network "public_network", ip: "192.168.1.100" **Ressourcenzuweisung**: Geeignete CPU und Arbeitsspeicher zuweisenruby

Basic port forwarding

config.vm.network "forwarded_port", guest: 80, host: 8080

Multiple ports

config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.network "forwarded_port", guest: 443, host: 8443 config.vm.network "forwarded_port", guest: 3306, host: 3306

Auto-correct port conflicts

config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true **Synchronisierte Ordner**: NFS für bessere Leistung auf macOS/Linux verwendenruby config.vm.provider "virtualbox" do|vb| vb.gui = true vb.memory = "4096" vb.cpus = 4 vb.name = "MyVM"

# Advanced settings vb.customize ["modifyvm", :id, "--vram", "128"] vb.customize ["modifyvm", :id, "--accelerate3d", "on"] vb.customize ["modifyvm", :id, "--clipboard", "bidirectional"] end **Box-Auswahl**: Minimale Basis-Boxen wählenruby config.vm.provider "vmware_desktop" do|vmware| vmware.vmx["memsize"] = "4096" vmware.vmx["numvcpus"] = "4" vmware.vmx["displayName"] = "MyVM" end **Snapshot-Management**: Snapshots für schnelle Rollbacks verwendenruby config.vm.provider "hyperv" do|hv| hv.memory = 4096 hv.cpus = 4 hv.vmname = "MyVM" end ```Provider-Optimierung: Anbieterspezifische Optimierungen konfigurieren

Sicherheit```ruby

Default synced folder (disabled)

config.vm.synced_folder ".", "/vagrant", disabled: true

Custom synced folder

config.vm.synced_folder "./src", "/var/www/html"

With options

config.vm.synced_folder "./app", "/var/www/html", owner: "www-data", group: "www-data", mount_options: ["dmode=775,fmode=664"] **SSH-Schlüssel**: SSH-Schlüssel statt Passwörter verwendenruby config.vm.synced_folder "./", "/vagrant", type: "nfs", nfs_udp: false, nfs_version: 4 **Netzwerkisolation**: Private Netzwerke verwenden, wenn möglichruby config.vm.synced_folder "./", "/vagrant", type: "smb", smb_username: "username", smb_password: "password" ``**Firewall**: Geeignete Firewall-Regeln konfigurierenvagrant ssh-config`Updates: Boxen und Vagrant aktuell halten