Search code examples
bashvagrantvagrant-provision

Is it possible to restart a machine when provisioning a machine using Vagrant and pickup where the script left off?


I was reading a tutorial in bash where they said to restart the machine, there was no option to restart a service directly, it was a matter of restarting the machine, and then there were more commands after that that still needed to be run when provisioning.

So is there any way to restart a box amid provisioning and then pick up where you left off after that?


Solution

  • As far as I know you can't have a single script/set of commands that would carry on where it left off if it attempts to restart the OS, such as:

      config.vm.provision "shell", inline: <<-SHELL
        echo $(date) > ~/rebootexample
        reboot
        echo $(date) >> ~/rebootexample
      SHELL
    

    In this example the second echo call would not be carried out.

    You could split the script/commands up and use a plugin such as vagrant reload.

    An example snippet of a Vagrantfile to highlight its possible use:

      # execute code before reload
      config.vm.provision "shell", inline: <<-SHELL
         echo $(date) > ~/rebootexample
      SHELL
    
      # trigger reload
      config.vm.provision :reload
    
      # execute code after reload
      config.vm.provision "shell", inline: <<-SHELL
         echo $(date) >> ~/rebootexample
      SHELL