Multiple providers in a single vagrant file?

2020-04-03 06:16发布

I've got a vagrant file that builds a local VM. I want to add the EC2 provider and have the option of either provisioning a local VM or one on EC2.

Can I create configs for multiple providers in the same Vagrantfile and somehow choose which to run when I do vagrant up?

7条回答
冷血范
2楼-- · 2020-04-03 06:23

Yes, you can specify multiple machines by using the config.vm.define method call, for example:

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

See: Defining multiple machines at Vagranup Docs and Providers

查看更多
beautiful°
3楼-- · 2020-04-03 06:28

To run VM locally you can run: vagrant up --provider=virtualbox and if you'd like to run VM using different provider then you can use: vagrant up --provider=aws

However, remember that you have to install appropriate provider plugin before you will use it.

查看更多
我想做一个坏孩纸
4楼-- · 2020-04-03 06:34

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
查看更多
时光不老,我们不散
5楼-- · 2020-04-03 06:36

You can choose which provider you want to run by --provider parameter.

Here is ruby code in Vagrantfile which can run different VM depending which provider you have chosen:

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

So by the default provider is virtualbox, but you can specify from the command line, like:

vagrant up --provider=aws 
查看更多
干净又极端
6楼-- · 2020-04-03 06:40

You can use a multi-vm environment, where every VM can be provisioned with a different provider and you can choose on commandline which one you want to vagrant up <machine>.

查看更多
Viruses.
7楼-- · 2020-04-03 06:41

From the Vagrant docs:

Multiple config.vm.provision methods can be used to define multiple provisioners. These provisioners will be run in the order they're defined.

eg.: First install puppet in the machine and then run some puppet manifests:


    $script = "
    wget http://apt.puppetlabs.com/puppetlabs-release-precise.deb
    sudo dpkg -i puppetlabs-release-precise.deb
    sudo apt-get update
    sudo aptitude -yy install puppet
    "


    config.vm.provision "shell", inline: $script

    config.vm.provision "puppet" do |puppet|
       puppet.manifests_path = "manifest/puppet"
       puppet.manifest_file = "init.pp"
    end

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

查看更多
登录 后发表回答