0

我真的希望能够在不更改 Vagrantfile 的情况下在 linux 机器和 mac 机器上运行单个虚拟机。我整理了一个 YAML 设置,我通常喜欢帮助配置盒子(或以后:盒子)。我在运行 Vagrantfile 时遇到问题。提前致谢!

我的调用是:

vagrant up --provider=virtualbox

或者

vagrant up --provider=vmware_fusion

YAML 配置文件看起来有点像这样:

CentOS64:
    ip: "192.168.33.110"
    properties:
        memory: 8192
        cpus: 4
    forwards:
        - host: 8080
          guest: 8080
        - host: 9115
          guest: 9115
    providers:
        virtualbox: "http://puppet-vagrant-boxes.puppetlabs.com/centos-64-x64-vbox4210-nocm.box"
        vmware_fusion: "https://dl.dropbox.com/u/5721940/vagrant-boxes/vagrant-centos-6.4-x86_64-vmware_fusion.box"
    provisions:
        pre:
            - provision: shell
              path: setup_users.sh
              privileged: true
            - provision: shell
              path: install_packages.sh
              privileged: true
            - provision: shell
              path: update_rcs.sh
              privileged: true
        post:
            ... installs application ...

Vagrant 文件看起来有点像这样:

require 'yaml'

settings = YAML.load_file "vagrant_config.yml"
default_provider = ENV['VAGRANT_DEFAULT_PROVIDER']

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  settings.each do |name, cfg|
    config.vm.define name do |agent|
      agent.vm.network :private_network, ip: cfg['ip']
      cfg['forwards'].each do |port|
        agent.vm.network "forwarded_port", guest: port['guest'], host: port['host']
      end
      agent.vm.provider :vmware_fusion do |v, override|
        override.vm.box = name + "_fusion"
        override.vm.box_url = cfg['providers']['vmware_fusion']
        cfg['properties'].each do |prop, val|
          if prop == "cpus"
            prop = "numvcpus"
          elsif prop == "memory"
            prop = "memsize"
          end
          override.vmx[prop] = val
        end
      end
      agent.vm.provider :virtualbox do |v, override|
        override.vm.box = name + "_vb"
        override.vm.box_url = cfg['providers']['virtualbox']
        cfg['properties'].each do |prop, val|
          if prop == "numvcpus"
            prop = "cpus"
          elsif prop == "memsize"
            prop = "memory"
          end
          agent.vm.customize ["modifyvm", :id, "--#{prop}", "#{val}"]
        end
      end
      cfg['provisions'].each do |prov|
        agent.vm.provision prov['provision'] do |p|
          # setup keys that do not directly map
          if prov.has_key?('recipes')
            prov['recipes'].each do |r|
              p.add_recipe r
            end
            prov.delete('recipes')
          end
          # setup 1:1 mapping keys
          prov.each do |key, val|
            if key != 'provision'
              p.instance_variable_set("@#{key}", val)
            end
          end
        end
      end
      if cfg.has_key?("shares")
        cfg['shares'].each do |sname, share|
          share['host'] = File.expand_path(share['host'])
          agent.vm.synced_folder share['host'], share['guest'], owner: share['owner'], group: share['group']
        end
      end
    end
  end
end

我已经为此工作了几天,但我仍然不能令人满意地启动和运行。我正在使用这个 Vagrant 文件来处理 Ruby。

我目前对上述设置的错误是:

>>> vagrant destroy --force; vagrant up
VM must be created before running this command. Run `vagrant up` first.
Bringing machine 'CentOS64' up with 'vmware_fusion' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* The following settings shouldn't exist: customize

Vagrant:
* Unknown configuration section 'vmx'.
4

1 回答 1

0

这个 vagrantfile 解决了以下问题:

require 'yaml'

settings = YAML.load_file "vagrant_config.yml"

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  settings.each do |name, cfg|
    config.vm.define name do |agent|
      agent.vm.box = name
      cfg['forwards'].each do |port|
        agent.vm.network "forwarded_port", guest: port['guest'], host: port['host']
      end
      agent.vm.provider :vmware_fusion do |v, override|
        v.box = name + "_fusion"
        v.box_url = cfg['providers']['vmware_fusion']
        cfg['properties'].each do |prop, val|
          if prop == "cpus"
            prop = "numvcpus"
          elsif prop == "memory"
            prop = "memsize"
          end
          v.vmx[prop] = val
        end
        v.vmx['fixed-address'] = cfg['ip']
      end
      agent.vm.provider :virtualbox do |v, override|
        override.vm.box = name + "_vb"
        override.vm.box_url = cfg['providers']['virtualbox']
        override.vm.network :private_network, ip: cfg['ip']
        cfg['properties'].each do |prop, val|
          if prop == "numvcpus"
            prop = "cpus"
          elsif prop == "memsize"
            prop = "memory"
          end
          v.customize ["modifyvm", :id, "--#{prop}", "#{val}"]
        end
      end
      cfg['provisions'].each do |prov|
        agent.vm.provision prov['provision'] do |p|
          # setup keys that do not directly map
          if prov.has_key?('recipes')
            prov['recipes'].each do |r|
              p.add_recipe r
            end
            prov.delete('recipes')
          end
          # setup 1:1 mapping keys
          prov.each do |key, val|
            if key != 'provision'
              p.instance_variable_set("@#{key}", val)
            end
          end
        end
      end
    end
  end
end
于 2014-12-05T15:37:29.727 回答