Vagrant provisioning scripts - vagrant

I'm pretty new to Vagrant and I've found out that the provision *.sh scripts (linked in my Vagrant file) needs to be updated. What is the best practice? Should I remove the box and re-create it? or rather "vagrant halt" and execute "vagrant provision" again?
Thanks

Depending on how your scipts work probably this would be enough:
vagrant provision
If your scripts require a vanilla machine to run, you need to destroy the already provisioned machine first:
vagrant destroy && vagrant up

Related

Can you perform a vagrant provision from inside the VM?

Usually you have to run vagrant provision from outside your VM to create the VM to begin with. I then do a vagrant ssh to inspect the resultant VM.
If I wish to make small tweaks to the VM (using chef zero recipes in my case), I have to either switch to an other tab that is on my physical host, or exit the SSH session. it would be nice if you could do this run-and-inspect inside the previously created VM.
Why I'm asking: I have too many terminal tabs open for development and am looking for ways to prune, and avoid mental context switching (not to mention trying to figure out which tab is which).
No, you can not run a vagrant provision from inside the same vagrant machine.
Vagrant is running on your host and provisioning the VM according to the specified vagrantfile. Any changes that you want to have applied during the provisioning must somehow come from the vagrantfile.
What you can do is modify a running vagrant machine in any way you want from inside the vagrant machine, and then export the VM using vagrant package to a new vagrant box which then can be used as base for new vagrant VMs.
PS: Not sure how you're dev environment looks like, but I suggest you look into terminal multiplexers like GNU screen or tmux, that might be able to help you with your "tab issues".

Upgrade php to 5.6 in Vagrant provisioning

I upgraded php to 5.6 within the Vagrant box 'trusty64', and also installed SOAP client. When I next update Vagrant I'm thinking it might overwrite these changes. Would I also need to change the provisioning in the vagrantfile, and if so what should I add?
When I next update Vagrant I'm thinking it might overwrite these
changes.
No, You would not loose anything if you upgrade vagrant. Once the VMs are created, vagrant will operate those VMs and upgrading vagrant will not impact the existing VM.
Basically, it works like this:
- when you run vagrant up, vagrant clone the box (which is VM files) and add the VM to VirtualBox
- after the VM have been created, vagrant "operates" (i.e. start, stop ...) the VirtualBox VM for you
Would I also need to change the provisioning in the vagrantfile
Thats necessary to change the provisioning if you plan to create more VM of this kind, or if you will destroy and recreate this VM; in this case the provisioning will run and you would need to have it updated.
and if so what should I add?
save all the commands you have run to run the upgrade and create a shell script out of it, might be the most simple option. You can also look at more advanced tool (puppet, ansible, chef .... that would do this job)

How do I package a vagrantfile so that it is used when 'vagrant init' is called on the box?

I know this is a stupid question, I'm still struggling to grok vagrant.
I run vagrant on a windows host, and I'm building Linux guest VMs using VirtualBox. My guest VM is running, and now I want to package it.
The Vagrant documentation says "A common misconception is that the --vagrantfile option will package a Vagrantfile that is used when vagrant init is used with this box. This is not the case. Instead, a Vagrantfile is loaded and read as part of the Vagrant load process when the box is used. For more information, read about the Vagrantfile load order."
Got it. But that's what I want to do! When I run "vagrant package --output myboxname.box", my carefully-crafted Vagrant file does not appear to be in the package. I test the box as follows (in Windows, after copying the new box):
cd \some_new_dir
vagrant box add myboxname.box --name boxname
vagrant init boxname
The new Vagrantfile is the generic vagrant version, with none of my changes.
When I
vagrant up
The vm comes up fine, but (not surprisingly) none of Vagrantfile directives have happened.
I know I'm missing something basic -- can someone please help me out?
Try copying the Vagrantfile from the directory you ran vagrant package in to the \some_new_dir directory. Then, without running vagrant init (because this will overwrite the Vagrantfile with an empty file), run vagrant up to use the Vagrantfile.

Vagrant: running ansible provisioning just while creating virtual machine?

Using Vagrant, is it possible to run my provisioning Ansible script just when vagrant up creates the virtual machine? I wan to provision just when the import sequence is run. I have a very slow step that imports the database. It isn't idempotent and I'd like it to run just the first time.
I can make a script using vagrant status and vagrant up --no-provision, but I believe there should be a more straightforward way.
I also can just run Ansible after the first vagrant up, but then I won't be able to brag to my friends that I create my server from scratch running just one command :-)
I think you are using a version older than 1.3.0;-)
Since Vagrant 1.3.0, vagrant up only run provisioning by default the first time it runs (after the import). Subsequent reload or up will need to explicitly specify the --provision flag, for example vagrant up --provision or just vagrant provision.
See the full changelog
The change => vagrant up will now only run provisioning by default the first time it is run. Subsequent reload or up will need to explicitly specify the --provision flag to provision. [GH-1776]

Which command to get vagrantfile changes on an instance

I made changes to my Vagrantfile (added config.vm.share_folder, config.vm.customize lines) and now I want them to show up on the box. Do I need to run
vagrant destroy && vagrant up
Or is it sufficient to run
vagrant reload
? I am unclear about whether vagrant reload will read anything from the Vagrantfile, since it seems to shutdown the VM and run the provisioner.
I don't think you need to destroy your VM. You can make changes to VM configuration when the VM is not running and powered down.
Try using vagrant halt and then vagrant up.
It should make the modifications to the VM as specified in Vagrantfile.
and you could also do vagrant up --no-provision to avoid re-provisioning as you bring up the VM.
and vagrant reload [vm-name] --no-provision should do the same trick.
vagrant reload is same as running vagrant halt followed by vagrant up please refer the doc for reload. Here is an excerpt from the doc
"This command is usually required for changes made in the Vagrantfile to take effect. After making any modifications to the Vagrantfile, a reload should be called." So in your case, I would suggest you to only use,
vagrant reload
Use:
vagrant reload --provision

Resources