24
config.vm.define :web do |web_config|
    web_config.vm.box = "saucy"
    web_config.vm.host_name = "web"
    web_config.vm.network "private_network", ip:"192.168.100.10"
end

config.vm.define :db do |db_config|
    db_config.vm.box = "saucy"
    db_config.vm.host_name = "db"
    db_config.vm.network "private_network", ip:"192.168.100.20"
end

config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "1024"]
    vb.customize ["modifyvm", :id, "--cpus", "2"]
end

I have config two VM, 'db' and 'web'. Could I set different memory size for different VM?

4 Answers 4

33

2016-08-31: Updated answer to include the whole Vagrantfile per @DarkForce request.

You can do that by moving the vm.provider definition inside each of the vm.define blocks. For example this configuration sets the memory to 2048MB for "web" and 1024MB for "db":

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.define :web do |web_config|
      web_config.vm.host_name = "web"
      web_config.vm.network "private_network", ip:"192.168.100.10"
      web_config.vm.provider :virtualbox do |vb|
          vb.customize ["modifyvm", :id, "--memory", "2048"]
          vb.customize ["modifyvm", :id, "--cpus", "2"]
      end
  end

  config.vm.define :db do |db_config|
      db_config.vm.host_name = "db"
      db_config.vm.network "private_network", ip:"192.168.100.20"
      db_config.vm.provider :virtualbox do |vb|
          vb.customize ["modifyvm", :id, "--memory", "1024"]
          vb.customize ["modifyvm", :id, "--cpus", "2"]
      end
  end
end

Note: This example (like many in the Vagrant docs) will only work for VirtualBox. If you wanted your Vagrantfile to also work with VMware or another provider, the customization parameters would be listed separately. For example:

x.vm.provider "vmware_fusion" do |v|
  v.vmx["memsize"] = "3000"
end
x.vm.provider :virtualbox do |v|
  v.customize ["modifyvm", :id, "--memory", "3000"]
end
Sign up to request clarification or add additional context in comments.

4 Comments

Tried this and it is not working. It works when I put it outside, before any machine definitions, but when I put it inside, I get errors. = /tmp/vagrant-shell: line 2: vb.memory: command not found ==> : /tmp/vagrant-shell: line 1: vb: command not found ==> /tmp/vagrant-shell: line 1: config.vm.provider: command not found ==> /tmp/vagrant-shell: line 3: end: command not found
I tested this answer and the answer of @a.wegmann and for me, both worked. Can you post your Vagrantfile?
@DarkForce I've updated the answer to include the whole Vagrant file; hope this helps. Note that I'm using box ubuntu/trusty64, but you could change that to whatever you're using.
@BrianC I meant that I tried both your answer and the answer just below and both worked for me. I was responding to user985366, but hey I got to meet a nice guy like you now :)
8

The best way is to use Ruby array of hashes like here - http://sysadm.pp.ua/linux/sistemy-virtualizacii/vagrantfile.html . You can define array like:

    servers=[
      {
        :hostname => "web",
        :ip => "192.168.100.10",
        :box => "saucy",
        :ram => 1024,
        :cpu => 2
      },
      {
        :hostname => "db",
        :ip => "192.168.100.11",
        :box => "saucy",
        :ram => 2048,
        :cpu => 4
      }
    ]

Then you just iterate each item in server array and define configs:

Vagrant.configure(2) do |config|
    servers.each do |machine|
        config.vm.define machine[:hostname] do |node|
            node.vm.box = machine[:box]
            node.vm.hostname = machine[:hostname]
            node.vm.network "private_network", ip: machine[:ip]
            node.vm.provider "virtualbox" do |vb|
                vb.customize ["modifyvm", :id, "--memory", machine[:ram]]

Comments

8

Vagrant also has convenient shortcuts for memory and CPU settings that take the following form:

config.vm.provider "virtualbox" do |v|
  v.memory = 1024
  v.cpus = 2
end

So in your case, and to give twice the CPUs and memory to DB boxes than web boxes, it would look something like:

config.vm.define :web do |web_config|
    web_config.vm.box = "saucy"
    web_config.vm.host_name = "web"
    web_config.vm.network "private_network", ip:"192.168.100.10"
    web_config.vm.provider :virtualbox do |vb|
        vb.memory = 1024
        vb.cpus = 2
    end
end

config.vm.define :db do |db_config|
    db_config.vm.box = "saucy"
    db_config.vm.host_name = "db"
    db_config.vm.network "private_network", ip:"192.168.100.20"
    db_config.vm.provider :virtualbox do |vb|
        vb.memory = 2048
        vb.cpus = 4
    end
end

2 Comments

the shortcuts are nice, but I had to replace "config" in "config.vm.provider" with "web_config" in the top block and "db_config" in the bottom block to get it to work correctly
Also it will not set the --name for the later one correctly.
5

I just tried to use the above solutions and realized, that in my environment all memory settings are the same. The code snippets seem to have a small typo - do not use config.vm.provider ... but use <yourConfigVariable>.vm.provider... to set the machine dependent values.

AFAIK the correct code would look like this:

config.vm.define :web do |web_config|
    web_config.vm.box = "saucy"
    web_config.vm.host_name = "web"
    web_config.vm.network "private_network", ip:"192.168.100.10"
    web_config.vm.provider :virtualbox do |vb|
        vb.customize ["modifyvm", :id, "--memory", "2048"]
        vb.customize ["modifyvm", :id, "--cpus", "2"]
    end
end

config.vm.define :db do |db_config|
    db_config.vm.box = "saucy"
    db_config.vm.host_name = "db"
    db_config.vm.network "private_network", ip:"192.168.100.20"
    db_config.vm.provider :virtualbox do |vb|
        vb.customize ["modifyvm", :id, "--memory", "1024"]
        vb.customize ["modifyvm", :id, "--cpus", "2"]
    end
end

1 Comment

this is the only anwer in this thread that worked for me; and I was also able to use the "vb.memory" and "vb.cpus" shortcuts instead of "vb.customize" given the answer by @ydaetskcoR

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.