How do I reboot a Vagrant guest from a provisioner?
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
Related videos on Youtube
DarkMorford
Updated on September 18, 2022Comments
-
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
andapt-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 almost 5 yearsThis has a major limitation: such trigger will only be run after
vagrant provision
command completes. It will not run after the firstvagrant up
is done provisioning. (see: github.com/hashicorp/vagrant/issues/10480) -
John Pancoast over 4 yearsIt 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 over 4 yearsNow 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 about 4 yearsThis is indeed a very clean approach (not depending on any plugin) after you upgrade kernel or guest additions
-
dragon788 almost 4 yearsRather 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.