Vagrant múltiples máquinas virtuales y múltiples carpetas sincronizadas
Con Vagrant podemos crear múltiples máquinas virtuales en un solo archivo Vagrantfile, también podemos sincronizar varias carpetas en una máquina virtual, la configuración es la siguiente:
$ProjectName1 = 'server1'
$ProjectName2 = 'server2'
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define "default" do |default|
default.vm.box = "Centos-6.6-minimal-x86_64"
default.vm.hostname = "#{$ProjectName1}"
default.vm.network "forwarded_port", guest: 80, host: 8000
default.vm.network :private_network, ip: "192.168.33.120"
default.vm.provider "virtualbox" do |v|
v.memory = 1024
v.cpus = 2
end
default.vm.synced_folder "../sitio1", "/var/www/sitio1", id: "vagrant-root1",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
default.vm.synced_folder "../sitio2", "/var/www/sitio2", id: "vagrant-root2",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
default.vm.synced_folder "../sitio3", "/var/www/sitio3", id: "vagrant-root3",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
default.vm.provision :shell, path: "bootstrap-centos.sh"
end
config.vm.define "web1" do |web1|
web1.vm.box = "ubuntu/trusty64"
web1.vm.hostname = "#{$ProjectName2}"
web1.vm.network "forwarded_port", guest: 80, host: 8001
web1.vm.network :private_network, ip: "192.168.33.121"
web1.vm.provider "virtualbox" do |v|
v.memory = 1024
v.cpus = 2
end
web1.vm.synced_folder "../sitio4", "/var/www/sitio4", id: "vagrant-root1",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
web1.vm.synced_folder "../sitio5", "/var/www/sitio5", id: "vagrant-root2",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
web1.vm.provision :shell, path: "bootstrap-ubuntu.sh"
end
end |
$ProjectName1 = 'server1'
$ProjectName2 = 'server2' VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define "default" do |default|
default.vm.box = "Centos-6.6-minimal-x86_64"
default.vm.hostname = "#{$ProjectName1}"
default.vm.network "forwarded_port", guest: 80, host: 8000
default.vm.network :private_network, ip: "192.168.33.120"
default.vm.provider "virtualbox" do |v|
v.memory = 1024
v.cpus = 2
end
default.vm.synced_folder "../sitio1", "/var/www/sitio1", id: "vagrant-root1",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
default.vm.synced_folder "../sitio2", "/var/www/sitio2", id: "vagrant-root2",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
default.vm.synced_folder "../sitio3", "/var/www/sitio3", id: "vagrant-root3",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
default.vm.provision :shell, path: "bootstrap-centos.sh"
end
config.vm.define "web1" do |web1|
web1.vm.box = "ubuntu/trusty64"
web1.vm.hostname = "#{$ProjectName2}"
web1.vm.network "forwarded_port", guest: 80, host: 8001
web1.vm.network :private_network, ip: "192.168.33.121"
web1.vm.provider "virtualbox" do |v|
v.memory = 1024
v.cpus = 2
end
web1.vm.synced_folder "../sitio4", "/var/www/sitio4", id: "vagrant-root1",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
web1.vm.synced_folder "../sitio5", "/var/www/sitio5", id: "vagrant-root2",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
web1.vm.provision :shell, path: "bootstrap-ubuntu.sh"
end
end
Cada máquina se define en esta linea de código.
config.vm.define "default" do |default| |
config.vm.define "default" do |default|
Por defecto cuando creamos normalmente una máquina virtual, Vagrant crea una máquina con el nombre default
, podemos revisar en la carpeta .vagrant/machines
, pero podemos poner cualquier nombre.
Para sincronizar carpetas:
default.vm.synced_folder "../sitio1", "/var/www/sitio1", id: "vagrant-root1",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"] |
default.vm.synced_folder "../sitio1", "/var/www/sitio1", id: "vagrant-root1",
owner: "vagrant",
group: "apache",
mount_options: ["dmode=775,fmode=664"]
Donde el valor id
debe ser diferente para cada carpeta.
Los comandos cambiarían con el nombre de la máquina, ejemplos: