8

我有一个构建本地 VM 的 vagrant 文件。我想添加 EC2 提供程序,并可以选择在 EC2 上配置本地 VM 或一个。

我可以在同一个 Vagrantfile 中为多个提供程序创建配置,并在我这样做时以某种方式选择要运行的配置vagrant up吗?

4

7 回答 7

8

您可以使用虚拟机环境,其中每个虚拟机都可以使用不同的提供程序进行配置,并且您可以在命令行上选择您想要的vagrant up <machine>

于 2013-06-12T08:00:59.033 回答
6

add box for each provider

> vagrant box add precise64 http://file.vagrantup.com/precise64.box
> vagrant box add precise64 http://file.vagrantup.com/precise64_vmware_fusion.box

and your Vagrantfile should look like

Vagrant.configure(2) do |config|
  config.vm.box="precise64"

  config.vm.provider "virtualbox" do |v|
    v.customize ["modifyvm", :id, "--memory", "2048"]
  end

  config.vm.provider "vmware_fusion" do |v|
    v.vmx["memsize"] = "2048"
  end
end

then create on each provider using following commands

> vagrant up --provider=virtualbox
> vagrant up --provider=vmware_fusion
于 2016-01-20T11:03:05.453 回答
2

您可以通过参数选择要运行的提供程序--provider

这是 ruby​​ 代码Vagrantfile,可以在其中运行不同的 VM,具体取决于您选择的提供商:

require 'getoptlong'

# Parse CLI arguments.
opts = GetoptLong.new(
  [ '--provider',     GetoptLong::OPTIONAL_ARGUMENT ],
)

provider='virtualbox'
begin
  opts.each do |opt, arg|
    case opt
      when '--provider'
        provider=arg
    end # case
  end # each
  rescue
end

# Vagrantfile API/syntax version.
Vagrant.configure(2) do |config|

  config.vm.hostname = "vagrant"
  config.vm.define "default-#{provider}"

  config.vm.provider "virtualbox" do |vbox, override|
    vbox.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
    vbox.name = "test.local"
    override.vm.box = "ubuntu/wily64"
    override.vm.network "private_network", ip: "192.168.22.22"
  end

  config.vm.provider :aws do |aws, override|
    aws.ami = "ami-7747d01e"
    aws.instance_type = "m3.medium"
    override.vm.box = "dummy"
    override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
    override.ssh.username = "ubuntu"
  end

end

因此,默认提供者是 virtualbox,但您可以从命令行指定,例如:

vagrant up --provider=aws 
于 2016-06-10T23:14:20.253 回答
1

要在本地运行 VM,您可以运行: vagrant up --provider=virtualbox 如果您想使用不同的提供程序运行 VM,则可以使用: vagrant up --provider=aws

但是,请记住,您必须先安装适当的提供程序插件,然后才能使用它。

于 2014-12-21T19:07:17.863 回答
1

请查看@tknerr 发布的这个要点,它适用于所有提供商,如 virtualbox、AWS,并与 vagrant-omnibus 插件结合管理。这是来自的代码Vagrantfile

#
# Vagrantfile for testing
#
Vagrant::configure("2") do |config|

  # the Chef version to use
  config.omnibus.chef_version = "11.4.4"

  def configure_vbox_provider(config, name, ip, memory = 384)
    config.vm.provider :virtualbox do |vbox, override| 
      # override box url
      override.vm.box = "opscode_ubuntu-13.04_provisionerless"
      override.vm.box_url = "https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-13.04_provisionerless.box"
      # configure host-only network
      override.vm.hostname = "#{name}.local"
      override.vm.network :private_network, ip: ip
      # enable cachier for local vbox vms
      override.cache.auto_detect = true

      # virtualbox specific configuration
      vbox.customize ["modifyvm", :id, 
        "--memory", memory,
        "--name", name
      ] 
    end
  end

  def configure_aws_provider(config)
    config.vm.provider :aws do |aws, override|
      # use dummy box
      override.vm.box = "aws_dummy_box"
      override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
      # override ssh user and private key
      override.ssh.username = "ubuntu"
      override.ssh.private_key_path = "#{ENV['HOME']}/.ssh/mccloud_rsa"

      # aws specific settings
      aws.access_key_id = "XXXX"
      aws.secret_access_key = "XXXXX"
      aws.ami = "ami-524e4726"
      aws.region = "eu-west-1"
      aws.availability_zone = "eu-west-1c"
      aws.instance_type = "m1.small"
      aws.security_groups = [ "mccloud", "http" ]
      aws.keypair_name = "mccloud-key-tlc"
    end
  end

  def configure_managed_provider(config, server)
    config.vm.provider :managed do |managed, override|
      # use dummy box
      override.vm.box = "managed_dummy_box"
      override.vm.box_url = "https://github.com/tknerr/vagrant-managed-servers/raw/master/dummy.box"

      # link with this server
      managed.server = server
    end
  end

#
# define a separate VMs for the 3 providers (vbox, aws, managed) 
# because with Vagrant 1.2.2 you can run a VM with only one provider at once
#
[:aws, :vbox, :esx].each do |provider|
  #
  # Sample VM per provider
  #
  config.vm.define :"sample-app-#{provider}" do | sample_app_config |

    case provider
    when :vbox
      configure_vbox_provider(sample_app_config, "sample-app", "33.33.40.10")
    when :aws
      configure_aws_provider(sample_app_config)
    when :esx    
      configure_managed_provider(sample_app_config, "33.33.77.10")  
    end

    sample_app_config.vm.provision :chef_solo do |chef|
      chef.cookbooks_path = [ './cookbooks/sample-app-0.1.0' ]
      chef.add_recipe "sample-app"
      chef.json = {
        :sample_app => {
          :words_of_wisdom => "Vagrant on #{provider} Rocks!"
        }
      }
    end
  end
end

end
于 2016-06-10T23:07:42.263 回答
-1

来自Vagrant 文档

可以使用多个 config.vm.provision 方法来定义多个配置器。这些配置程序将按照它们定义的顺序运行。

例如:首先在机器上安装 puppet,然后运行一些 puppet 清单:

    $脚本 = "
    wget http://apt.puppetlabs.com/puppetlabs-release-precise.deb
    sudo dpkg -i puppetlabs-release-precise.deb
    sudo apt-get 更新
    sudo aptitude -yy 安装木偶
    "


    config.vm.provision “外壳”,内联:$script

    config.vm.provision "puppet" 做 |puppet|
       puppet.manifests_path = "清单/木偶"
       puppet.manifest_file = "init.pp"
    结尾

    config.vm.provision "shell", inline: "echo Second shell provisioner"

于 2013-10-25T13:40:40.880 回答
-1

是的,您可以使用config.vm.define方法调用指定多台机器,例如:

Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: "echo Hello"

  config.vm.define "web" do |web|
    web.vm.box = "apache"
  end

  config.vm.define "db" do |db|
    db.vm.box = "mysql"
  end
end

请参阅:在 Vagranup Docs and Providers中定义多台机器

于 2016-06-10T23:20:14.370 回答