packer-cn/website/source/intro/getting-started/vagrant.html.md

81 lines
3.4 KiB
Markdown
Raw Normal View History

2013-06-28 08:37:13 -04:00
---
2015-07-22 22:31:00 -04:00
description: |
Packer also has the ability to take the results of a builder (such as an AMI or
plain VMware image) and turn it into a Vagrant box.
layout: intro
next_title: Remote Builds and Storage
next_url: '/intro/getting-started/remote-builds.html'
page_title: Vagrant Boxes
prev_url: '/intro/getting-started/parallel-builds.html'
...
2013-06-28 08:37:13 -04:00
# Vagrant Boxes
2015-07-22 22:31:00 -04:00
Packer also has the ability to take the results of a builder (such as an AMI or
2016-01-14 15:31:19 -05:00
plain VMware image) and turn it into a [Vagrant](https://www.vagrantup.com) box.
2013-06-28 08:37:13 -04:00
This is done using [post-processors](/docs/templates/post-processors.html).
These take an artifact created by a previous builder or post-processor and
transforms it into a new one. In the case of the Vagrant post-processor, it
takes an artifact from a builder and transforms it into a Vagrant box file.
2015-07-22 22:31:00 -04:00
Post-processors are a generally very useful concept. While the example on this
getting-started page will be creating Vagrant images, post-processors have many
interesting use cases. For example, you can write a post-processor to compress
artifacts, upload them, test them, etc.
2013-06-28 08:37:13 -04:00
2015-07-22 22:31:00 -04:00
Let's modify our template to use the Vagrant post-processor to turn our AWS AMI
into a Vagrant box usable with the [vagrant-aws
plugin](https://github.com/mitchellh/vagrant-aws). If you followed along in the
previous page and setup DigitalOcean, Packer can't currently make Vagrant boxes
for DigitalOcean, but will be able to soon.
2013-06-28 08:37:13 -04:00
## Enabling the Post-Processor
Post-processors are added in the `post-processors` section of a template, which
we haven't created yet. Modify your `example.json` template and add the section.
Your template should look like the following:
2015-07-22 22:31:00 -04:00
``` {.javascript}
2013-06-28 08:37:13 -04:00
{
"builders": ["..."],
"provisioners": ["..."],
2013-06-28 08:37:13 -04:00
"post-processors": ["vagrant"]
}
```
2013-06-28 08:37:13 -04:00
In this case, we're enabling a single post-processor named "vagrant". This
2015-07-22 22:31:00 -04:00
post-processor is built-in to Packer and will create Vagrant boxes. You can
always create [new post-processors](/docs/extend/post-processor.html), however.
2013-06-28 08:37:13 -04:00
The details on configuring post-processors is covered in the
[post-processors](/docs/templates/post-processors.html) documentation.
Validate the configuration using `packer validate`.
## Using the Post-Processor
2015-07-22 22:31:00 -04:00
Just run a normal `packer build` and it will now use the post-processor. Since
Packer can't currently make a Vagrant box for DigitalOcean anyway, I recommend
2015-07-22 22:31:00 -04:00
passing the `-only=amazon-ebs` flag to `packer build` so it only builds the AMI.
The command should look like the following:
2013-06-28 08:37:13 -04:00
2015-07-22 22:31:00 -04:00
``` {.text}
2013-06-28 08:37:13 -04:00
$ packer build -only=amazon-ebs example.json
```
2015-07-22 22:31:00 -04:00
As you watch the output, you'll notice at the end in the artifact listing that a
Vagrant box was made (by default at `packer_aws.box` in the current directory).
Success!
2013-06-28 08:37:13 -04:00
2017-02-21 18:27:55 -05:00
But where did the Amazon EBS builder artifact go? When using post-processors,
Vagrant removes intermediary artifacts since they're usually not wanted. Only
the final artifact is preserved. This behavior can be changed, of course.
Changing this behavior is covered [in the
documentation](/docs/templates/post-processors.html).
2015-07-22 22:31:00 -04:00
Typically when removing intermediary artifacts, the actual underlying files or
resources of the artifact are also removed. For example, when building a VMware
image, if you turn it into a Vagrant box, the files of the VMware image will be
deleted since they were compressed into the Vagrant box. With creating AWS
images, however, the AMI is kept around, since Vagrant needs it to function.