How to use packer with box file? - vagrant

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.

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

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

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

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 workflow

Hi i was trying to understand what exactly is done when running
vagrant up
my reason for that is that in my case we need to install a lot of utilities.
i.e Version control tools, build tools, ide, etc...
which takes a lot of time.
so actually i wanted a 'box' with all those tools.
After i have clean environment and got all tools, i would like to make CI for our product.
If i will reinstall all utilities it should take a lot of time. so what i am actually need is just installing and testing our product.
How should i handle that ??
create my own box? does the command reinstall all utilities when we make CI ??
what i actually need are 2 processes :
1.installing utilities for my vm. (once a month)
2.test our product (each commit\push to version control)
how can i achieve that ?
For the first time, vagrant up will create a new VM for you, pulling the box image if needed, and it will provision it with what you configured in the Vagrantfile. In the provision configuration, you can tell Chef or Puppet to install all the utilities and tools that you need.
When you suspend or halt the VM, the next time you do a vagrant up it will only bring that VM back up. It will not install or try to provision it again.
You can force it with vagrant up --provision or just vagrant provision.
This usually works well in a development environment.
In a CI environment, it may not be possible to have the VM already provision, forcing you to run the provisioning step every time. You can achieve what you need packaging your own box with the tools already installed, essentially creating a golden or base image.
Just be extra careful so that the CI environment don't differ for what you have in production.
All depend on the setting in Vagrantfile
you have modules folder to put all puppet modules, manifests folder with site.pp and Vagrantfile as below under same place.
Give you a sample of Vagrantfile I used mostly.
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "precise64"
config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box "
config.vm.provision :puppet do |puppet|
puppet.module_path = "modules"
puppet.manifests_path = "manifests"
puppet.manifest_file = "site.pp"
end
config.vm.define :www do |config|
config.vm.host_name = "www.example.com"
config.vm.network :private_network, ip: "192.168.1.2"
end
config.vm.define :db1 do |config|
config.vm.host_name = "db1.example.com"
config.vm.network :private_network, ip: "192.168.1.4"
end
end
So after run vagrant up www or vagrant up db1 it will start the box with puppet apply directly. You can understand this way as puppet masterless
Vagrant just make the box up running from linux image defined in config.vm.box_url as a simple fresh linux box, and mount your local folder to new box's /vagrant folder, then hand over to puppet. How the server to be provisioned, will be depended on site.pp (define applications on each node) and puppet modules. The command is similar as
puppet apply --modulepath /vagrant/module /vagrant/manifests/site.pp
So if your puppet modules are fine, your new box will automatically have all utilities and products installed. Then you run vagrant ssh www or vagrant ssh db1, you can login it and start working.
You can put your local folder with moduels, manifests folders and Vagrantfile to version control (such as git). So developers can clone the git repository to their own computer easily.

Resources