How to rebuild vagrantfile - vagrant

I vagrant up a project, but accidentally delete the Vagrantfile folder, the VM is still in VirtualBox, how to rebuild the vagrantfile and assign it to the same VM.

You cannot rebuild Vagrantfile. But you can associate your folder with existing vm.
Short step-by-step (based on this link):
create file named .vagrant
run $ VBoxManage list vms in your terminal
get id hash for desired machine.
put into .vagrant this code: {"active":{"default":"YOUR-ID-HERE"}}
So you'll get something like this: {"active":{"default":"ec426377-dbe6-4be7-4751-766956e44958"}}
run $ vagrant init (this will create new Vagrantfile)
run $ vagrant up (this will associate your desired folder with running vm)
run $ ssh-keygen -t rsa -f .vagrant/machines/default/virtualbox/private_key
run $ ssh-copy-id -i .vagrant/machines/default/virtualbox/private_key.pub -p 2222 vagrant#127.0.0.1, password: vagrant

I don't think there is a way to do that (create a Vagrantfile from an existing VM). From the perspective of Vagrant virtual machines being temporary in nature (build, use, destroy), you're best bet may be to recreate the Vagrantfile again and start over.
If there is something on the VM that you want or need, you could access it directly via VirtualBox, then copy over to the new VM as needed.

Related

Forgetting a VM in Vagrant

I've started a VM on Google Compute Engine using Vagrant with the vagrant-google provider. I no longer wish to control the VM using Vagrant, but I would like it to keep running without me interrupting it.
What's the proper way to have Vagrant "forget" about this machine?
so the instance has been installed on GCE and you can control directly from there.
Locally you can delete the .vagrant folder that vagrant created in the folder when you launch it
After this to remove references you can run vagrant global-status --prune which will remove invalid entries and clean vagrant conf file from this machine

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.

Distributing vagrant VM locally

I have set up a vagrant vm on my machine. (Virtualbox)
I would like to be able to share this VM amongst other machines however would like to avoid having to download the image repeatedly.
I tried Exporting the Appliance and copying the Vagrantfile folder onto a different machine, however when i tried to connect using vagrant ssh it could not find it. I assume this is because of non-matching UUID's. How can I go around this?
Vagrant version 1.6.3
The solution was to use Vagrant Package
vagrant package --base <Name Of VM on virtualbox>
This creates a package.box file in your current directory.
You then share that package.box file, and on the new computers you execute:
vagrant init package.box
in the directory where the Vagrantfile will be created

How do I remove a Vagrant box from global-status, after deleting that box from the filesystem?

I deleted a folder containing a running Vagrant box before realising it was still running.
How can I delete it from Vagrant (global-status) now?
I already removed the Virtualbox VM.
To discard old boxes listed in vagrant global-status (eg. you deleted the folder containing the .vagrant dir from the filesystem) you just need to run:
vagrant global-status --prune
You might need to remove the Virtual Machine directly from your provider (VMWare, Virtualbox, ..) control interface.
I believe that the new filename of the global data is ~/.vagrant.d.
So you can run rm -r ~/.vagrant.d to delete all user data for a fresh start.
For my case: Where kitchen and .kitchen/ folder may contain hidden vagrant files create by
$ kitchen create
I need to go further than either kitchen destroy or vagrant destroy or even vagrant destroy machineID ( from vagrant global-status - which gives a list of vagrant instances across your user space. )
In my case, I had to blow away the vagrant machine lock files contained in the users .vagrant hidden directory.
$ rm -r ~/.vagrant/data
And in my case I need to sudo the above command.
Then I finally got to a clean
$ vagrant global-status
list
First you use vagrant global-status to list all the vagrant machines and their status and ids.
then you can use:
vagrant destroy -f the_id_of_the_machine
after that if you run vagrant global-status again you will find that the id you have specified in the vagrant destroy -f command has been removed from the list.
and then you might need to restart your machine However these commands will not affect your boxes.
The above commands didn't remove the old box in my case on a Windows machine. I had already removed the .vagrant folder and the box from the virtualbox provider folder but the box was still in the vagrant global-status list.
As mentioned in the comment given by a vagrant up command:
If you believe this message is in error, please check the process
listing for any "ruby" or "vagrant" processes and kill them. Then
try again.
So I killed ruby.exe from the process list and did a subsequent vagrant global-status --prune.

Vagrant package new box (from standard one)

I've got a standard Vagrant precise64 box. I want to add a number of packages, say git and subversion and then repackage the box. How can I use the package command to create a new box?
I know package is a plugin in v2. This doc was remove in v2 and I'm not sure how it applies
http://docs-v1.vagrantup.com/v1/docs/base_boxes.html
If I try in my directory containing the vagrant machine and the Vagrant file I get:
$ vagrant package boxname --output test.box --vagrantfile Vagrantfile
The machine with the name 'boxname' was not found configured for
this Vagrant environment.
Edit:
The following works
$ vagrant package --output vagrant_example.box
Which leaves me with the following questions. What is the boxname for? What does the base command do? Thanks.
I've hand crafted a couple of Vagrant base boxes (as well as using Veewee). AFAIR, the command format hasn't changed since 1.0.x.
In your case, you may have to init and up the box first, do your job and then re-package it. So the boxname (--base NAME) is the name in VirtualBox, you can get the name by using VBoxManage list vms.
vagrant package --output vagrant_example.box works because it is packaging the current vagrant box (you are in the directory where the Vagrantfile resides).
See the docs http://docs.vagrantup.com/v2/cli/package.html
--base NAME - Instead of packaging a VirtualBox machine that Vagrant manages, this will package a VirtualBox machine that VirtualBox manages. NAME should be the name or UUID of the machine from the VirtualBox GUI.

Resources