Can I see boot screen on vagrant up? - vagrant

For default if you starting vagrant (with vagrant up) then you can only see Vagrant messages. How can I print out boot informations from the box I'm just booting with?
EDIT: I want to test some init scripts and see how they behave.

On that command prompt? Not easily. What you can do is get the vm provider to run in headed mode so you can see the virtualised console, e.g. for virtualbox add:
config.vm.provider :virtualbox do |vb|
# Don't boot with headless mode
vb.gui = true
end
Indise the Vagrant.configure block.

Related

How do you set the VM name via 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

How to run scripts automatically after doing vagrant ssh?

I am new to Vagrant but good in Docker.
In Vagrant I am aware of the fact that
config.vm.provision :shell,path: "bootstrap.sh", run: 'always'
in the Vagrantfile will provision vagrant box while doing vagrant up. With this, the vagrant box interactive console appears after the intended provisioning is done.
But I need to configure in such a way that, first the control goes in to vagrant box console and then the intended script is up and running. Because my requirement is to run a script automatically post vagrant up and not to run a bootstrapped script.
In analogy with Docker, my question can be seen as
what is the Vagrant equivalent for CMD in Dockerfile ?
You can look at vagrant triggers. You can run dedicated script/command after each specific vagrant command (up, destroy ...)
For example
Vagrant.configure("2") do |config|
# Your existing Vagrant configuration
...
# start apache on the guest after the guest starts
config.trigger.after :up do |trigger|
trigger.run_remote = {inline: "service apache2 start"}
end
end

Made changes to Vagrantfile - now my vagrant will not start

I made some changes to my Vagrantfile. All I did was to add more processors and ram to the config.
Before (and commented out):
....
#config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# vb.cpus = "2"
#end
....
After:
....
config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
vb.gui = true
#
# # Customize the amount of memory on the VM:
vb.memory = "1024"
vb.cpus = "2"
end
....
I rebooted the machine and got this error:
The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in the
'poweroff' state. Please verify everything is configured
properly and try again.
If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve.
For example, if you're using VirtualBox, runvagrant upwhile the
VirtualBox GUI is open.
The primary issue for this error is that the provider you're using
is not properly configured. This is very rarely a Vagrant issue.
So I changed my Vagrantfile back to how it was originally (as above).
I still can't boot the machine with vagrant up. What have I done wrong?
When I do have the UI enabled. It just opens and closes without me being able to see it.

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).

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