jaitaiwan
By:
jaitaiwan

Is there a way to use vagrant to spin up multiple droplets?

April 16, 2015 1.2k views
DigitalOcean Scaling Linux Commands Configuration Management Ubuntu

I understand that you can use Vagrant to spawn a droplet and do configuration and installation, but what I want to know is if I can use vagrant to spawn multiple droplets without having to have duplicated vagrant projects?

1 Answer

With Vagrant, each folder and Vagrantfile represents a separate project. If you want to launch multiple copies of the same setup, you'll need to duplicate the project. If your project needs multiple droplets (e.g. separate web and database servers), you can use a Vagrant multi-machine environment. For instance, this would create two different droplets, one named "db" and one named "web."

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = 'digital_ocean'
  config.vm.box_url = "https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box"
  config.ssh.private_key_path = '/path/to/id_rsa'

  config.vm.define "db" do |db|
    config.vm.provider :digital_ocean do |provider|
      provider.token = 'YOUR_DO_TOKEN'
      provider.image = 'ubuntu-14-04-x64'
      provider.region = 'nyc3'
      provider.size = '512mb'
      provider.ssh_key_name = 'key_name'
    end
  end

  config.vm.define "web" do |web|
    config.vm.provider :digital_ocean do |provider|
      provider.token = 'YOUR_DO_TOKEN'
      provider.image = 'ubuntu-14-04-x64'
      provider.region = 'nyc3'
      provider.size = '512mb'
      provider.ssh_key_name = 'key_name'
    end
  end

end
  • Hi,

    I have tried your solution but it doesn't work for me:

    There was an issue with the request made to the Digital Ocean
    API at:
    
    Path: /v2/droplets
    URI Params: {}
    
    The response status from the API was:
    
    Status: 401
    Response: {"id"=>"unauthorized", "message"=>"Unable to authenticate you."}
    

    Following vagrant tutorial for multi-machine environments on

    Vagrant Multi-machine environment

    I have realized that after defining, you have to use the name you provide. So your code should look like:

    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    
      config.vm.box = 'digital_ocean'
      config.vm.box_url = "https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box"
      config.ssh.private_key_path = '/path/to/id_rsa'
    
      config.vm.define "db" do |db|
        db.vm.provider :digital_ocean do |provider|
          provider.token = 'YOUR_DO_TOKEN'
          provider.image = 'ubuntu-14-04-x64'
          provider.region = 'nyc3'
          provider.size = '512mb'
          provider.ssh_key_name = 'key_name'
        end
      end
    
      config.vm.define "web" do |web|
        web.vm.provider :digital_ocean do |provider|
          provider.token = 'YOUR_DO_TOKEN'
          provider.image = 'ubuntu-14-04-x64'
          provider.region = 'nyc3'
          provider.size = '512mb'
          provider.ssh_key_name = 'key_name'
        end
      end
    
    end
    
Have another answer? Share your knowledge.