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

后端 未结 4 2174
北荒
北荒 2021-02-12 11:47

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 t

4条回答
  •  失恋的感觉
    2021-02-12 12:21

    One trick you can employ is to send restart signal and save rest of the provisioning work as a script to be run on boot:

    config.vm.provision "shell", inline: <<-SHELL
      echo "Do your thing... DONE"
    cat <<-RCLOCAL | sed -s 's_^      __' > /etc/rc.local
          #!/bin/bash
          echo "This will be run once on next boot and then it's destroyed and never run again"
          rm /etc/rc.local
    RCLOCAL
      chmod o+x /etc/rc.local
      shutdown -r now #restart
    SHELL
    

    This was tested to work on debian 9, so you may need to enable services or find another way to get your code bootsrapped to run on the next boot if you're running something else.

    Unfortunately you can't simply do:

    config.vm.provision "shell", inline: "shutdown -r now"
    config.vm.provision "shell", inline: "echo 'hello world'"
    
    results in ==>
    The SSH connection was unexpectedly closed by the remote end. This
    usually indicates that SSH within the guest machine was unable to
    properly start up. Please boot the VM in GUI mode to check whether
    it is booting properly.
    

提交回复
热议问题