Search code examples
vagrantvagrantfilepackervagrant-provision

Save changes Vagrantfile on file.box


After I created my 'Vagrantfile' from my file 'test.box', I'm going to edit the file 'Vagrantfile' inserting for example:

config.vm.provision: shell, path: './test.sh'

Done this, how do I save the changes to the file test.box?


Solution

  • I am not sure I fully understand the question, but let me explain:

    Boxes are the package format for Vagrant environments, you can read more about boxes from the documentation (https://docs.vagrantup.com/v2/boxes.html)

    From this box, vagrant will create a VM using the Vagrantfile that you have defined. If this Vagrantfile defines provisioning steps, those will be run when you initialized the vm (first time you run vagrant up, you can also define a provisioner to run every time you start the vm)

    To run a provisioner every time you start the vm, you need to have

    config.vm.provision :shell, :path => "./test.sh", :run => 'always'
    

    Note when you run vagrant halt to stop the VM, you only shutdown the system so everything you have install remains on the vm, next time you run vagrant up all the things you have created in the VM will be available. vagrant destroy does completely wipe out all files from vm so when you run vagrant up after, the vm will be created from scratch

    so now, if you have done changes to the vm and would like to use that as a new box, you need to package the vm using vagrant package, you can read more about package. This will produce a new box that you can use in your new Vagrantfile.