vagrant中配置多虚拟机


利用 vagrant 创建第二个虚拟机时,按照创建第一个虚拟机时的方法,发现不好用

C:\vgrant>vagrant box add node2 centos65-x86_64-20140116.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'node2' (v0) for provider:
box: Unpacking necessary files from: file://C:/vgrant/centos65-x86_64-20140116.box
box:
The box you're attempting to add already exists. Remove it before
adding it again or add it with the `--force` flag.

Name: box1
Provider: virtualbox
Version: 0

C:\vgrant>vagrant init node2
`Vagrantfile` already exists in this directory. Remove it before
running `vagrant init`.

C:\vgrant>vagrant up node2
The machine with the name 'node2' was not found configured for
this Vagrant environment.

此时找到操作系统中的 Vagrantfile 文件

打开文件之后,在文件中添加

config.vm.define "node2" do |node2|                   ---定义虚拟机名称
node2.vm.box = "centos65-x86_64-20140116.box"          ---定义虚拟机使用的镜像 
end

保存后,在cmd中直接vagrant up启动即可

配置虚拟机ip                

通过在Vagrant文件上更改配置来实现

 default.vm.network  :private_network, ip: "192.168.1.11"

配置虚拟机hostname

default.vm.hostname="node1"

整个配置文件如下:

Vagrant.configure("2") do |config|
config.vm.provider "virtualbox" do |v|
v.memory = 2048
end

config.vm.define "default" do |default|
default.vm.box = "centos65-x86_64-20140116.box"
default.vm.network :private_network, ip: "192.168.1.11"
default.vm.hostname="node1"
end

config.vm.define "node2" do |node2|
node2.vm.box = "centos65-x86_64-20140116.box"
node2.vm.network :private_network, ip: "192.168.1.22"
node2.vm.hostname="node2"
end

config.vm.define "node3" do |node3|
node3.vm.box = "centos65-x86_64-20140116.box"
node3.vm.network :private_network, ip: "192.168.1.33"
node3.vm.hostname="node3"
end

end