From the vagrant documentation:
vagrant halt [name|id]
Let's say vagrant global-status outputs the following:
id name provider state directory
------------------------------------------------
6e16e1a envname virtualbox running D:/git/envname
So, from outside the directory containing Vagrantfile I should be able to halt this machine either with
vagrant halt 6e16e1a #works!
As well as with
vagrant halt envname #doesn't work!
The error message:
A Vagrant environment or target machine is required to run this command. Run vagrant init to create a new Vagrant environment. Or, get an ID of a target machine from vagrant global-status to run this command on. A final option is to change to a directory with a Vagrantfile and to try again.
This is the same output as when I vagrant halt in a directory with no Vagrantfile in it.
So, Can I vagrant halt by name, if yes, how?
Vagrant 2.2.14 on Windows 10
Vagrantfile excerpt:
config.vm.hostname = "envname"
config.vm.define "envname"
I couldn't find a "clean" solution to this problem so I wrote a little bash function to get the box id from the box name.
function vgid() {
BOX_NAME=$1
BOX_ID=$(vagrant global-status --prune | grep $BOX_NAME | awk '{print $1}')
echo $BOX_ID
}
Using this function I can now use the box's name in commands like this:
vagrant up $(vgid name)
It's not my favorite solution but it does work and I prefer it to copy-pasting ids from the global-status output.
Related
I'm trying to export my Vagrant machine. I ran vagrant package mymachine on computer A and then copied the resulting package.box file to computer B. Then ran vagrant box add package.box --name mymachine (on computer B). Everything seemed to work fine. Then I ran vagrant up mymachine (using the Vagrantfile I copied from computer A) and vagrant ssh mymachine and found out none of the files in the original machine were present on the new one. Any ideas as to what I might be doing wrong?
If you copied the Vagrantfile from the Computer A, its likely it does not reference your new box. Edit the Vagrantfile on Computer B and change this part
Vagrant.configure("2") do |config|
config.vm.box = "mymachine"
...
end
when you'll boot the instance from this VM (you might need to destroy the current VM and recreate it) it will create a new VM from the newmachine VM
I'll trying this command :
vagrant init hashicorp/percise32 && vagrant up
but this error happen
The box 'hashicorp/percise32' could not be found or
could not be accessed in the remote catalog. If this is a private
box on HashiCorp's Atlas, please verify you're logged in via
`vagrant login`. Also, please double-check the name. The expanded
URL and error message are shown below:
URL: ["https://atlas.hashicorp.com/hashicorp/percise32"]
Error: The requested URL returned error: 404 Not Found
This is complete valid syntax, you just had a misspell on the name
vagrant init hashicorp/precise32 && vagrant up
note precise32 vs percise32
vagrant init hashicorp/precise32 creates a vagrantfile with the name of the box. When you'll run vagrant up from this Vagrantfile, vagrant will create the VM from the box referenced from the file, if it does not find it, it will download the box
try the following
vagrant box add hashicorp/precise32
and then try
vagrant init
You should have your box referenced in your vagrant file:
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "hashicorp/precise32"
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "https://atlas.hashicorp.com/hashicorp/boxes/precise32/versions/1.0.0/providers/virtualbox.box"
and then
vagrant up
if that doesnt help though not recomended try this on the first step :
vagrant box add hashicorp/precise32 https://atlas.hashicorp.com/hashicorp/boxes/precise32/versions/1.0.0/providers/virtualbox.box
Hope it helps
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?
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.
My goal is to run a file shell every time I run the command 'vagrant up / vagrant provision'.
To do this I did the following:
I created (with Packer) the file 'test.box'
With the command 'init test.box vagrant' I create Vagrantfile.
In this step, I edit the file Vagrantfile inputting, for example:
config.vm.provision: shell, path: './file.sh'
So that every time I run the command 'vagrant up / vagrant provision' he performs "file.sh".
My problem is that I do not understand how I can save this change.
If I understand you correctly.
You may take your vagrantfile, make all needed changes
and add additional section to your packer.json file:
"post-processors": [
{
"type": "vagrant",
"keep_input_artifact": false,
"compression_level": 1,
"vagrantfile_template": "MyCustom.vagrantfile",
"output": "box/box_with_custom_vagrantfile.box"
}
]
I'm building a vagrant .box file using packer. I've got something like this in my packer .json config:
"post-processors": [
//SNIP
"include": [
"./choco.ps1",
"./vagrantUp.ps1",
]
]
It's building the .box, and if I manually untar the file I can see those are included in there. When I end up running "vagrant box add Mybox http://whatever" I can even see them in my ~/.vagrant.d/boxes/MyBox/0/virtualbox folder.
~ $ ls ~/.vagrant.d/boxes/MyBox/0/virtualbox/
Vagrantfile metadata.json
box.ovf packer-virtualbox-iso-1424829938-disk1.vmdk
choco.ps1 vagrantUp.ps1
Also, in my packer vagrant template I have a line:
config.vm.provision "shell", path: "vagrantUp.ps1"
Next, I want to initialize this machine. I did the following:
~ $ cd ~/Vagrant
Vagrant $ Vagrant mkdir MyBox; cd MyBox
MyBox $ vagrant init MyBox
MyBox $ ls
VagrantFile
This file looks like the basic vagrant default, but at the top it mentions config.vm.box = "MyBox".
But then if I vagrant up, I get the following error:
* `path` for shell provisioner does not exist on the host system: /Users/me/Vagrant/MyBox/vagrantUp.ps1
It looks to me like the VagrantFile in /Vagrant/MyBox is referencing the VagrantFile in ~/.vagrant.d/, which is good, that's what I want. But then it's still using the paths relative to /Vagrant/MyBox, when the files are actually relative to ~/.vagrant.d/
Am I missing a step that tells vagrant to copy those files to the instance directory? Or am I referencing them incorrectly in my vagrant template?
In the Vagrantfile within the box, you can refer to other files within the box using __FILE__ to the get path to that box Vagrantfile, and then use path instead of inline with the shell provisioner so that the script gets uploaded to the VM and then executed:
Vagrant.configure("2") do |config|
# ... other configuration
box_root = File.expand_path(File.dirname(__FILE__))
config.vm.provision "shell",
path: File.join(box_root, "vagrantUp.ps1")
end
The scripts will be located relative to the Vagrantfile, so on the host machine. Check out the path section in here: http://docs.vagrantup.com/v2/provisioning/shell.html
It clearly states:
Relative paths, such as above, are expanded relative to the location of the root Vagrantfile for your project.
I think that there is no functionality in vagrant init or vagrant init that can extract the scripts and copy it to the local folder where the Vagrantfile resides. I have no idea why they have this functionality in the Packer Vagrant post-processor, under include:
Paths to files to include in the Vagrant box. These files will each be copied into the top level directory of the Vagrant box (regardless of their paths). They can then be used from the Vagrantfile.
I am using vagrant with puppet provisioning. The provisioning setting in my vagrantfile looks like:
config.vm.provision "puppet" do |puppet|
puppet.manifests_path = "puppet/manifests"
puppet.hiera_config_path = "puppet/hiera.yaml"
puppet.module_path = "../puppet/modules"
puppet.manifest_file = "site.pp"
end
I have another VM which is not managed by vagrant and want to apply the puppet configuration on it. I want to use the exact 'puppet apply' command that is being used by vagrant.
Can someone please tell me the exact 'puppet apply' command being used by vagrant?
It's open source, you can always look at the source: plugins/provisioners/puppet/provisioner/puppet.rb. The relevant method is run_puppet_apply. And/or you can enable verbose logging on a test provisioning and inspect the log to see the command line.
I have another VM which is not managed by vagrant and want to apply the puppet configuration on it. I want to use the exact 'puppet apply' command that is being used by vagrant.
That's not going to work. The exact vagrant puppet provisioning command contains references to the temporary attached folders where the vagrant files are.
Can someone please tell me the exact 'puppet apply' command being used by vagrant?
Nobody will be able to do that because the exact command is specific to your environment.
My recommendation is to extract the command applied to one of your existing VMs from the log, and the use this as a starting point to build your own, manual command. The relevant command items are the module paths (which will contain references to temporary shared folders, basically making ../puppet/modules visible in the VM), your hiera file (which is 'uploaded' into the VM into a temporary file) and the FACTER defines, if any.
If you need direct answer, the command is
sudo puppet apply --hiera_config puppet/hiera.yaml --modulepath=../puppet/modules puppet/manifests/site.pp
Normally the current folder (with module and manifest folders and others) will be mounted under /vagrant on guest instance. After vagrant up and vagrant ssh to that instance, you can cd to folder /vagrant and run above puppet apply command to prove if the command runs fine or not.