How do I reboot a Vagrant guest from a provisioner?

10,854

Solution 1

This is exactly what the vagrant-reload plugin is for. After installing it simply add config.vm.provision :reload at the point in the provisioning you want to reload.

Solution 2

If you only want to reboot after provisioning is complete (not between provisioning steps), you can use a trigger to achieve this:

config.trigger.after [:provision] do |t|
  t.name = "Reboot after provisioning"
  t.run = { :inline => "vagrant reload" }
end

This has the advantage of not requiring a plugin to be installed.

More information on triggers here.

Solution 3

Assuming your guest VM can handle it, using the built in Shell Provisioner reboot may be a good choice. More info on that is here. Look for reboot in changelog for when this feature was added and improved.

Example block:

config.vm.provision :shell do |shell|
    shell.privileged = true
    shell.inline = 'echo rebooting'
    shell.reboot = true
end
Share:
10,854

Related videos on Youtube

DarkMorford
Author by

DarkMorford

Updated on September 18, 2022

Comments

  • DarkMorford
    DarkMorford over 1 year

    Relative newcomer to Vagrant here, trying to eliminate going through the install process manually every time I want to spin up a new Ubuntu VM.

    My usual process, after completing the install, is to update all of the machine's software with apt-get update and apt-get dist-upgrade -y, then rebooting it to ensure all the upgrades take effect. To that end, I made a Vagrantfile like so:

    Vagrant.configure("2") do |config|
      config.vm.box = "ubuntu/xenial64"
      config.vm.provider "virtualbox" do |vb|
        vb.memory = "4096"
      end
    
      config.vm.provision "shell", inline: <<-SHELL
        apt-get update
        apt-get dist-upgrade -y
        shutdown -r now
      SHELL
    end
    

    This mostly does the job--I can see the output from the VM as the packages update--but after it reboots and I vagrant ssh into it, none of my files appear in /vagrant. I'm assuming the shared folder isn't being remounted for some reason.

    I guess my first question is: Does it make sense to update the system like this as part of a shell provisioner? And if so, how can I reboot the guest and still have the /vagrant share mounted?

  • Ad N
    Ad N almost 5 years
    This has a major limitation: such trigger will only be run after vagrant provision command completes. It will not run after the first vagrant up is done provisioning. (see: github.com/hashicorp/vagrant/issues/10480)
  • John Pancoast
    John Pancoast over 4 years
    It seems as though you can have triggers that run for many events as seen here: github.com/hashicorp/vagrant/issues/… E.g., config.trigger.after :up, :provision do |t|... etc. I edited the code to reflect that. Let me know if I messed anything up.
  • John Pancoast
    John Pancoast over 4 years
    Now that I think of it, you wouldn't want to do the :up. This means that every time you run vagrant up it will also reload the machine. It's harmless, but unnecessary.
  • webofmars
    webofmars about 4 years
    This is indeed a very clean approach (not depending on any plugin) after you upgrade kernel or guest additions
  • dragon788
    dragon788 almost 4 years
    Rather than explicitly reloading, you could possibly check whether the VM actually needs a restart and then trigger a reboot/reload. Typically exit code 1603 denotes a reboot is required, and chocolatey has a warning around a reboot being required/requested as well.