使用循环优化 Vagrant 文件代码

Optimise Vagrant file code with loop

我有 vagrant 文件,它使我能够在 VirtualBox

中 运行 Win 7 和 Win 8 OS
 Vagrant.configure("2") do |config|
  # Win7 VM
  config.vm.define "win7" do |win7|
    win7.vm.box_url = "file:///C:/V_W_S/windows_7_virtualbox.box"
    win7.vm.box = "windows_7_virtualbox"
    win7.vm.communicator = "winrm"  
    win7.vm.boot_timeout = 600
    win7.vm.graceful_halt_timeout = 600
    win7.vm.network "public_network", auto_config: true
  win7.vm.provider "virtualbox" do |vb|
       vb.customize ["modifyvm", :id, "--usb", "on"]
       vb.customize ["modifyvm", :id, "--usbehci", "on"]
       vb.customize ["modifyvm", :id, "--accelerate3d", "on"]       
       vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
       vb.customize ["modifyvm", :id, "--memory", "4096"]
       vb.customize ["modifyvm", :id, "--vram", "256"]
       vb.cpus = 2
       vb.gui = true
       vb.name = "Win_7_x64_Mult"
end
end
  # Windows 8 VM
  config.vm.define "win8" do |win8|
    win8.vm.box_url = "file:///C:/V_W_S/Windows8_x64.box"
    win8.vm.box = "Windows8_x64"
    win8.vm.communicator = "winrm"  
    win8.vm.boot_timeout = 600
    win8.vm.graceful_halt_timeout = 600
    win8.vm.network "public_network", auto_config: true
  win8.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--usb", "on"]
    vb.customize ["modifyvm", :id, "--usbehci", "on"]
    vb.customize ["modifyvm", :id, "--accelerate3d", "on"]       
    vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
    vb.customize ["modifyvm", :id, "--memory", "4096"]
    vb.customize ["modifyvm", :id, "--vram", "256"]
    vb.cpus = 2
    vb.gui = true
    vb.name = "Win_8_x64_Mult"
end
end
end

但我想使用循环来减少代码量。例如在我的 vagrant 文件中 config 值是相同的。区别是 win.vm.define 值、win.vm.box 和 win7.vm.box_url 值。如何压缩 Vagrantfile 的数量?

我会做这样的事情

Vagrant.configure("2") do |config|

  config.vm.communicator = "winrm" 
  config.vm.boot_timeout = 600
  config.vm.graceful_halt_timeout = 600
  config.vm.network "public_network", auto_config: true

  # Win7 VM
  config.vm.define "win7" do |win7|
    win7.vm.box_url = "file:///C:/V_W_S/windows_7_virtualbox.box"
    win7.vm.box = "windows_7_virtualbox"
    configure_provider(win7, "Win_7_x64_Mult")
  end

  # Windows 8 VM
  config.vm.define "win8" do |win8|
    win8.vm.box_url = "file:///C:/V_W_S/Windows8_x64.box"
    win8.vm.box = "Windows8_x64"
    configure_provider(win8, "Win_8_x64_Mult")
  end

end

def configure_provider (node, node_name)
  node.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--usb", "on"]
    vb.customize ["modifyvm", :id, "--usbehci", "on"]
    vb.customize ["modifyvm", :id, "--accelerate3d", "on"]       
    vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
    vb.customize ["modifyvm", :id, "--memory", "4096"]
    vb.customize ["modifyvm", :id, "--vram", "256"]
    vb.cpus = 2
    vb.gui = true
    vb.name = node_name
  end
end