Multiple providers in a single vagrant file?

后端 未结 7 1403
一整个雨季
一整个雨季 2021-01-18 01:50

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 co

7条回答
  •  再見小時候
    2021-01-18 02:31

    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 
    

提交回复
热议问题