Search code examples
vagrantvirtualboxvagrantfile

Configure multiple hosts with inheritance, overriding memory


I have a Vagrantfile that provisions 6 virtual machines. They are all similar, except of the necessary memory size (and the ip address).

I would like to set a default for RAM size and change it where necessary, process1 and process2 should get 6GB.

I tried to change .memory using several prefixes (app, v, ...) but obviously didn't get the syntax right. I also tried app.customize ["modifyvm", :id, "--memory", "6144"] but vagrant validate complained about this.

At present the file looks like this:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure( VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "geerlingguy/centos7"
  config.vm.provider :virtualbox do |v|
    v.memory = 2048
    v.linked_clone = true
  end

  config.vm.define "process1" do |app|
    app.vm.hostname = "process1"
    app.vm.network :private_network, ip: "192.168.60.11"
  end

  config.vm.define "process2" do |app|
    app.vm.hostname = "process2"
    app.vm.network :private_network, ip: "192.168.60.12"
  end

  config.vm.define "index1" do |app|
    app.vm.hostname = "index1"
    app.vm.network :private_network, ip: "192.168.60.13"
  end

  config.vm.define "index2" do |app|
    app.vm.hostname = "index2"
    app.vm.network :private_network, ip: "192.168.60.14"
  end

  config.vm.define "quorum" do |app|
    app.vm.hostname = "quorum"
    app.vm.network :private_network, ip: "192.168.60.15"
  end

  config.vm.define "control" do |app|
    app.vm.hostname = "control"
    app.vm.network :private_network, ip: "192.168.60.21"
  end

end

Solution

  • You need to override into each block

    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure( VAGRANTFILE_API_VERSION) do |config|
      config.vm.box = "geerlingguy/centos7"
      config.vm.provider :virtualbox do |v|
        v.memory = 2048
        v.linked_clone = true
      end
    
      config.vm.define "process1" do |app|
        app.vm.hostname = "process1"
        app.vm.network :private_network, ip: "192.168.60.11"
        app.vm.provider :virtualbox do |v|
          v.memory = 6144
        end
      end
    
      config.vm.define "process2" do |app|
        app.vm.hostname = "process2"
        app.vm.network :private_network, ip: "192.168.60.12"
        app.vm.provider :virtualbox do |v|
          v.memory = 6144
        end
      end
    
      config.vm.define "index1" do |app|
        app.vm.hostname = "index1"
        app.vm.network :private_network, ip: "192.168.60.13"
      end
    
      config.vm.define "index2" do |app|
        app.vm.hostname = "index2"
        app.vm.network :private_network, ip: "192.168.60.14"
      end
    
      config.vm.define "quorum" do |app|
        app.vm.hostname = "quorum"
        app.vm.network :private_network, ip: "192.168.60.15"
      end
    
      config.vm.define "control" do |app|
        app.vm.hostname = "control"
        app.vm.network :private_network, ip: "192.168.60.21"
      end
    
    end