2019-12-17 05:25:56 -05:00
|
|
|
---
|
|
|
|
layout: guides
|
|
|
|
sidebar_current: hcl
|
|
|
|
page_title: Getting started configuring Packer with HCL2 files
|
|
|
|
---
|
|
|
|
|
|
|
|
# Introduction to Packer HCL2
|
|
|
|
|
|
|
|
-> **Note:** Starting from version **1.5.0** Packer can read HCL2 files.
|
|
|
|
|
2020-03-02 17:05:42 -05:00
|
|
|
<%= partial "partials/guides/hcl2-beta-note" %>
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
It is not necessary to know all of the details of the HCL syntax in order to
|
|
|
|
use Packer, and so this page summarizes the most important details to get you
|
|
|
|
started. If you are interested, you can find a [full definition of HCL
|
|
|
|
syntax](https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md) in
|
|
|
|
the HCL native syntax specification.
|
|
|
|
|
|
|
|
## Arguments and Blocks
|
|
|
|
|
|
|
|
The HCL syntax is built around two key syntax constructs: arguments and blocks.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
# block
|
|
|
|
source "amazon-ebs" "example" {
|
|
|
|
|
|
|
|
# argument
|
|
|
|
ami_name = "abc123"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Comments
|
|
|
|
|
|
|
|
The HCL language supports three different syntaxes for comments:
|
|
|
|
|
|
|
|
* ```#``` begins a single-line comment, ending at the end of the line.
|
|
|
|
* ```//``` also begins a single-line comment, as an alternative to ```#```.
|
|
|
|
* ```/*``` and ```*/``` are start and end delimiters for a comment that might
|
|
|
|
span over multiple lines.
|
|
|
|
|
|
|
|
## Multi-line strings
|
|
|
|
|
|
|
|
A multi-line string value can be provided using heredoc syntax.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
variable "long_key" {
|
|
|
|
type = "string"
|
|
|
|
default = <<EOF
|
|
|
|
This is a long key.
|
|
|
|
Running over several lines.
|
|
|
|
It could be super handy for a boot_command.
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
```
|
2019-12-19 08:58:44 -05:00
|
|
|
|
|
|
|
## Building blocks can be split in files
|
|
|
|
|
|
|
|
Currently Packer offers the `source` and the `build` root blocks. These two
|
2019-12-19 09:28:29 -05:00
|
|
|
building blocks can be defined in any order and a `build` can import one or more
|
2019-12-19 08:58:44 -05:00
|
|
|
`source`. Usually a `source` defines what we currently call a builder and a
|
|
|
|
`build` can apply multiple provisioning steps to a source. For example:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
# folder/sources.pkr.hcl
|
2019-12-19 09:28:29 -05:00
|
|
|
source "amazon-ebs" "example-1" {
|
2019-12-19 08:58:44 -05:00
|
|
|
ami_name = "example-1-ami"
|
|
|
|
}
|
|
|
|
|
2019-12-19 09:34:26 -05:00
|
|
|
source "virtualbox-iso" "example-2" {
|
|
|
|
boot_command = <<EOF
|
|
|
|
<esc><esc><enter><wait>
|
2020-03-02 17:05:42 -05:00
|
|
|
/install/vmlinuz noapic
|
2019-12-19 09:34:26 -05:00
|
|
|
...
|
|
|
|
EOF
|
2019-12-19 08:58:44 -05:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
# folder/build.pkr.hcl
|
|
|
|
build {
|
|
|
|
sources = [
|
2019-12-19 09:28:29 -05:00
|
|
|
"source.amazon-ebs.example-1",
|
2019-12-19 09:34:26 -05:00
|
|
|
"source.virtualbox-iso.example-2"
|
2019-12-19 08:58:44 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
provisioner "shell" {
|
|
|
|
inline = [
|
|
|
|
"echo it's alive !"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|