What is the point of do |variable| in Vagrantfile?

23 Views Asked by At

Can someone explain to me the difference b/w these two options, if any? What is the preferred way and why?

Rn I have this:

BoxName = "crbienes-test"
Vagrant.configure("2") do |config|
  config.vm.box = "Ubuntu-23.04-LAMP"
  config.vm.define BoxName
  config.vm.hostname = BoxName
  config.vm.network "private_network", ip: "192.168.70.11"
    
  config.vm.provider "virtualbox" do |vb|
    vb.name = BoxName
    vb.linked_clone = true # saves time/space by not copying the box
  end

But recently I found an example that does this:

Vagrant.configure("2") do |config|

  config.ssh.username = "vagrant"
  config.ssh.insert_key = false
  config.ssh.host = "127.0.0.1"
  config.ssh.guest_port = 22
  config.vm.box_check_update = false # Why is this one not inside the following define-do clause?

  config.vm.define "testbox" do |testbox|
   testbox.vm.box = BOX_BASE
   testbox.vm.hostname = "testbox"
   testbox.vm.network :forwarded_port, guest: 22, host: LOCAL_SSH_PORT, id: "ssh"

   testbox.vm.provider :virtualbox do |virtualbox|
     virtualbox.customize ["modifyvm", :id, "--memory", BOX_RAM_MB]
     virtualbox.customize ["modifyvm", :id, "--cpus", BOX_CPU_COUNT]
   end
 end
end

Here it puts certain definitions inside a define "testbox" do |testbox| clause.

Following this example, my Vagrantfile would look like this:

BoxName = "crbienes-test"
Vagrant.configure("2") do |config|
  config.vm.define BoxName do |box|
    box.vm.box = "Ubuntu-23.04-LAMP"
    box.vm.hostname = BoxName
    box.vm.network "private_network", ip: "192.168.70.11"
  end

  config.vm.provider "virtualbox" do |vb|
    vb.name = BoxName
    vb.linked_clone = true # saves time/space by not copying the box
  end
end

In the same manner, can I simplify this:

config.vm.provider "virtualbox" do |vb|
    vb.name = BoxName
    vb.linked_clone = true
  end

into this (removing the do |x| part)?

config.vm.provider "virtualbox"
config.vm.name = BoxName
config.vm.linked_clone = true

Now I don't know which format to follow and if there's any functional difference.

0

There are 0 best solutions below