我的Vagrantfile

用了 Vagrant 批量创建很多个虚拟机,用于 salt 使用学习,Cassandra、Spark 集群搭建测试等。

Vagrantfile 内容如下

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

domain = 'example'
def_network = '172.31.0'
def_ram = '1536'
os_ubuntu = "ubuntu/trusty64"
os_centos6 = "bento/centos-6.7"
os_centos7 = "bento/centos-7.2"

nodes = [
  { :hostname => 'master', :ip => def_network+'.11', :box => os_centos6, :ports => { 22 => 3211 } },
  { :hostname => 'minion1', :ip => def_network+'.21', :box => os_centos6, :ports => { 22 => 3221 } },
  { :hostname => 'minion2', :ip => def_network+'.22', :box => os_centos6, :ports => { 22 => 3222 } },
  { :hostname => 'minion3', :ip => def_network+'.23', :box => os_ubuntu, :ports => { 22 => 3223 } },
  { :hostname => 'minion4', :ip => def_network+'.24', :box => os_ubuntu, :ports => { 22 => 3224 } },

  { :hostname => 'centos7', :ip => def_network+'.31', :box => os_centos7, :ram => 2048, :ports => { 22 => 3231, 80 => 80 } },
  { :hostname => 'ubuntu16', :ip => def_network+'.32', :box => os_ubuntu, :ram => 4096, :ports => { 22 => 3232 } },
  { :hostname => 'centos6', :ip => def_network+'.33', :box => os_centos6, :ram => 2048, :ports => { 22 => 3233 } },
]

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  nodes.each do |node|
    config.vm.define node[:hostname] do |node_config|
      node_config.vm.provider 'virtualbox' do |vb|
        vb.gui = false
        vb.name = node[:hostname]
        vb.memory = node[:ram] ? node[:ram] : def_ram;
        vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/"+node[:hostname], "1"]
      end
      node_config.ssh.insert_key = false
      node_config.ssh.password = "vagrant"
      node_config.vm.box = node[:box]
      node_config.vm.hostname = node[:hostname] + '.' + domain
      node_config.vm.network 'private_network', ip: node[:ip], virtualbox__intnet: 'salt'
      node_config.vm.synced_folder 'D:\projects', '/home/vagrant/projects', mount_options: ["dmode=755,fmode=755"] #, disabled: true
      if node.key?(:ports)
        config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true" # 禁用默认的ssh转发,再添加自定义转发
        node[:ports].each do | guestport, hostport |
          node_config.vm.network 'forwarded_port', guest: guestport, host: hostport
        end
      end
    end
  end
end

遇到的问题,主要是 node_config.vm.synced_folder 这个挂载host主目录时候,有时候出现 GuestAdditions 版本不相同的错误,网上说安装 vagrant 插件就可以解决

vagrant plugin install vagrant-winnfsd
vagrant plugin install vagrant-vbguest

如果host机器是windows操作系统,而虚拟机是Linux,那么共享windows里面的目录挂载到Linux中,那在Linux里面不可以创建软链接,会报错。

下面这个配置就是为了解决这一问题:

vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/"+node[:hostname], "1"]

或者,在virtualbox安装目录中执行如下命令:

VBoxManage setextradata YOURVMNAME VBoxInternal2/SharedFoldersEnableSymlinksCreate/YOURSHAREFOLDERNAME 1  

其中,YOURVMNAME为虚拟机中linux系统的名称,YOURSHAREFOLDERNAME 为共享的目录名称。