emyl / vagrant-triggers

Allow the definition of arbitrary scripts that will run on the host before and/or after Vagrant commands.
MIT License
546 stars 35 forks source link

Access machine ID from trigger #29

Closed krlmlr closed 9 years ago

krlmlr commented 9 years ago

How do I access the ID of the current virtual machine inside a trigger? The following works, but there must be a better way:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.provider "virtualbox" do |vb, override|
    # Use VBoxManage to customize the VM. For example to change memory:
    vb.customize ["modifyvm", :id, "--memory", "1536"]

    override.trigger.after :halt, :append_to_path => ENV["VBOX_INSTALL_PATH"] do
      run "VBoxManage storageattach " +
        File.read(".vagrant/machines/default/virtualbox/id") +
        " --storagectl SATA --port 1 --medium none"
    end
  end
end

Also on SO: http://stackoverflow.com/q/26985575/946850.

emyl commented 9 years ago

You can use @machine.id. Here is an example:

config.trigger.after :halt do
  info "Machine ID: #{@machine.id}"
end