How do you set the VM name via Vagrant? - vagrant

I've set up 4 VMs in Vagrant and now trying to set the name of a VM in Vagrant as I don't just want to ssh into the default VM.
I can't find any docs on the vagrant website but found this:
How to change Vagrant 'default' machine name?
However, when I try:
config.vm.define "foohost"
and do a vagrant up I get:
There are errors in the configuration of this machine. Please fix
the following errors and try again:
vm:
* The following settings shouldn't exist: define

I suspect you actually wrote
config.vm.define = "foohost"
rather than
config.vm.define "foohost"
as that would explain the error message. (Best to show several lines of actual source if you can.)

as I don't just want to ssh into the default VM.
so when you use multiple VM, you can tell vagrant which VM you want to ssh by default, using the following
config.vm.define "foohost", primary: true do |foohost|
...
end
then when you run vagrant ssh you will ssh by default in this VM.
To ssh into the other MV, you will need to specify the VM name

Related

Is "vagrant up" possible from different local directories with custom URLs?

I have two directories under /var/www/html, named vlp1 and vlp2 with Vagrant setup. If I run vagrant up separately in these folders the default local URL is vagrant.local for both. But vagrant.local/ always loads site from vlp1 folder.
As it should be, vagrant global-status shows that I have two virtual machines running under these directories. I require to run both sites side by side and to do this the only option I see is to have separate URLs. I believe there is a way to assign different URLs to different sites but don't have any idea how!
What I need to do to accomplish the above so that my sites run like local.vlp1.com and local.vlp2.com or may be like vagrant.vlp1 and vagrant.vlp2?
According to project instructions I have to keep two sites completely separated in two folders and have to use separate Vagrantfiles.
OS: Ubuntu 14.04 64 bit
Installed VitualBox version: 5.1
Installed Vagrant version: 1:1.9.4
Thank you!
UPDATE
Went through the following steps as advised by Henri:
Step 1:
$ vagrant global-status
Output:
id name provider state directory
------------------------------------------------------------------------
eb9b569 default virtualbox running /var/www/html/vlp1
190608c default virtualbox running /var/www/html/vlp2
Step 2: Did a graceful shutdown on vlp2
$ vagrant halt 190608c
Step 3: Output of vagrant global-status now is
id name provider state directory
-------------------------------------------------------------------------
eb9b569 default virtualbox running /var/www/html/vlp1
190608c default virtualbox poweroff /var/www/html/vlp2
Step 4:
$ sudo gedit /var/www/html/vlp2/Vagrantfile
if CONF['ip'] == "dhcp"
config.vm.network :private_network, type: "dhcp", hostsupdater: "skip"
else
config.vm.network :private_network, ip: "192.168.2.5"
end
Step 5: Re up'd vlp2 machine
$ vagrant up 190608c
Step 6:
$ vagrant global-status now shows both machine are back to running state again.
id name provider state directory
------------------------------------------------------------------------
eb9b569 default virtualbox running /var/www/html/vlp1
190608c default virtualbox running /var/www/html/vlp2
Step 7:
In /etc/hosts added following entry
192.168.2.5 vagrant.vlp2
Finally I tried vagrant.vlp2/ in browser but ended up with the following message:
This site can’t be reached
http://vagrant.vlp2/ is unreachable.
As I understand you're using multiple VM so the easiest in this case is to assign each of the VM a static IP (that must be different) so you do so in Vagrantfile
Vagrant.configure("2") do |config|
...
config.vm.network "private_network", ip: "192.168.10.x"
...
end
and you use another IP for the second VM
Vagrant.configure("2") do |config|
...
config.vm.network "private_network", ip: "192.168.20.y"
...
end
and then you update your /etc/hosts file with the information
192.168.10.x vagrant.vlp1
192.168.20.y vagrant.vlp2

Vagrant cannot vagrant up the box packaged from ubuntu xenial64 16.04

I have a custom vagrant box based on the offcial box ubuntu 16.04.
I simplly run like this to get the packaged box.
vagrant init ubuntu/xenial64; vagrant up --provider virtualbox
vagrant up
vagrant ssh # enter the virtual machine and do some custom change on it
vagrant halt
vagrant package --vagrantfile Vagrantfile --output custom_ubuntu1604.box
and then i copy the file custom_ubuntu1604.box to another directory, i use the box like this:
vagrant box add ubuntu1604base custom_ubuntu1604.box
vagrant init ubuntu1604base
vagrant up # at this point the machine will be stopped at "Started Journal Servie"
my new virtualbox machine base on the new packaged box will stop at:
the screenshot
And finally it timed out:
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within the
configured ("config.vm.boot_timeout" value) time period.
If you look above, you should be able to see the error(s) that Vagrant
had when attempting to connect to the machine. These errors are
usually good hints as to what may be wrong.
If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes. Verify
that authentication configurations are also setup properly, as well.
If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
Try to set config.vm.boot_timeout in Vagrantfile more than default e.x.600. From my experience I found out it take a long time at the first time to connecting guest machine.
For example
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.provider "virtualbox"
config.vm.boot_timeout = 600
end

Vagrant keeps trying to use use AWS plugin. How to stop?

I'm just trying to vagrant up a standard Ubuntu image for the first time. My company has set up Vagrant on my laptop already and installed some plugins for AWS. When I try to run vagrant up on my personal Ubuntu image, with a separate Vagrantfile, I get the following error:
There are errors in the configuration of this machine. Please fix
the following errors and try again:
AWS Provider:
* An access key ID must be specified via "access_key_id"
* A secret access key is required via "secret_access_key"
* An AMI must be configured via "ami" (region: #{region})
I'm not trying to connect to AWS. I'm just trying to set up my first personal image on my laptop.
Make sure you actually have VirtualBox installed. I witnessed this error message today and it was because I had the vagrant-aws plugin installed but not VirtualBox. Installing VirtualBox was all that was needed to fix the problem.
If for some reason that still fails, then, per the OP's comment above, try using vagrant up's --provider option:
vagrant up --provider virtualbox
By default Vagrant will go through all of the config.vm.provider and will choose the first usage provider, so you should add virtualbox before aws:
config.vm.provider "virtualbox" do |v|
v.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
v.memory = 4096
v.cpus = 2
end
or choose different provider manually either by --provider as already mentioned, or VAGRANT_DEFAULT_PROVIDER variable:
VAGRANT_DEFAULT_PROVIDER=virtualbox vagrant up
or use config.vm.provider with no configuration in order to set the order in your Vagrantfile like:
Vagrant.configure("2") do |config|
# ... other config up here
# Prefer VirtualBox Fusion before VirtualBox
config.vm.provider "virtualbox"
config.vm.provider "aws"
end
See: Basic provider usage at vagrantup.com
If you need to use AWS provider, these error means you need to set-up the right credentials by using aws configure command, so ~/.aws/credentials can be created with your aws_access_key_id and aws_secret_access_key values. For AMI configuration, check README file of the plugin (basically you need to add aws.ami into your Vagrant file).

Vagrant ssh using username and password

I want to connect with a vagrant machine with different user instead of Vagrant also want to use another username and password instead of using keys. Also, I want to know is it possible to use ssh vagrant vm from another vm running in same machine. If so, how to do that?
Vagrant has a few options (see full doc https://docs.vagrantup.com/v2/vagrantfile/ssh_settings.html) :
Vagrant.configure("2") do |config|
config.ssh.username = "user"
config.ssh.password = "password"
end
note indeed, you need to make sure those users exist on the guest os (generally most vagrant box are created with vagrant user)
To have the connection between your different VMs, you can easily do that if you assign fix IP to the VM.
Vagrant.configure("2") do |config|
config.vm.network :private_network, ip: "192.168.45.15"
end
when you connect to your second VM, you can run ssh vagrant#192.168.45.15 and it will ssh to the first VM

How to use packer with box file?

I have a vagrantfile using a box on top of virtualbox with a provision script.
Now I am trying to use packer to output a box already after provision.
However I cannot find a builder to use the ".box" file I already have. What am I doing wrong?
I just got a solution to this tiny little problem (convert a vagrant .box file to .ova for use by packer):
Create a vm using the .box file as a base. I use this Vagrantfile, with box opscode-centos-7.0:
$provisioning_script = <<PROVISIONING_SCRIPT
adduser packer
echo "packer" | passwd packer --stdin
echo "packer ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/packer
PROVISIONING_SCRIPT
Vagrant.configure(2) do |config|
config.vm.box = "opscode-centos-7.0"
config.ssh.insert_key = false
config.vm.provider "virtualbox" do |v|
v.name = "packer-base"
end
config.vm.provision :shell, inline: $provisioning_script
end
run vagrant up
run vagrant halt
run vboxmanage export --ovf20 -o packer-base.ova packer-base
run vagrant destroy
This also creates the packer user with a default password so that packer can easily connect to the instance to do stuff. Also note the insert_key parameter that will prevent replacing the vagrant default insecure key with a secure one and allow subsequent vagrant setups to properly connect via SSH to the new images (after packer is done).
Packer out-of-the-box doesn't support using Vagrant boxes as input (yet).
But there is a custom plugin, see this comment.
If you want to build a vagrant box that runs with provider virtualbox, have a look here.
However, it takes an iso or ovf as input, not a vagrant box.
Have a look at these templates to get you started using the virtualbox builder with packer.
Make sure your run the post-processor to convert the virtualbox vm into a vagrant box.

Resources