packer-cn/website/content/docs/templates/hcl_templates/index.mdx

78 lines
2.8 KiB
Plaintext
Raw Normal View History

2019-12-18 10:57:36 -05:00
---
page_title: HCL Templates
sidebar_title: HCL Templates
2019-12-18 10:57:36 -05:00
description: |-
Packer uses text files to describe infrastructure and to set variables.
These text files are called Packer _configurations_ and are
written in the HCL2 HashiCorp Configuration Language.
2019-12-18 10:57:36 -05:00
---
# HCL Configuration Language
2020-05-26 09:29:47 -04:00
`@include 'from-1.5/beta-hcl2-note.mdx'`
2019-12-18 10:57:36 -05:00
Packer uses the Hashicorp Configuration Language - HCL - designed to allow
2019-12-20 05:23:48 -05:00
concise descriptions of the required steps to get to a build file. This page
describes the features of HCL2 exhaustively, if you would like to give a quick
try to HCL2, you can also read the quicker [HCL2 getting started
guide](/guides/hcl).
2019-12-18 10:57:36 -05:00
## Builds
The main purpose of the HCL language is defining builds and sources. All other
language features exist only to make the definition of builds more flexible and
convenient.
`packer build` takes one argument. When a directory is passed, all files in the
folder with a name ending with `.pkr.hcl` or `.pkr.json` will be parsed using
the HCL2 format. When a file ending with `.pkr.hcl` or `.pkr.json` is passed it
2020-03-18 18:46:47 -04:00
will be parsed using the HCL2 schema. For every other case; the _JSON only_ old
2019-12-18 10:57:36 -05:00
packer schema will be used.
## Arguments, Blocks, and Expressions
The syntax of the HCL language consists of only a few basic elements:
```hcl
source "amazon-ebs" "main" {
ami_name = "main-ami"
}
<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
# Block body
<IDENTIFIER> = <EXPRESSION> # Argument
}
```
- _Blocks_ are containers for other content and usually represent the
configuration of some kind of object, like a source. Blocks have a
_block type,_ can have zero or more _labels,_ and have a _body_ that contains
any number of arguments and nested blocks. Most of Packer's features are
controlled by top-level blocks in a configuration file.
- _Arguments_ assign a value to a name. They appear within blocks.
- _Expressions_ represent a value, either literally or by referencing and
combining other values. They appear as values for arguments, or within other
expressions.
For full details about Packer's syntax, see:
- [Configuration Syntax](/docs/templates/hcl_templates/syntax)
- [Expressions](/docs/templates/hcl_templates/expressions)
2019-12-18 10:57:36 -05:00
## Code Organization
The HCL language uses configuration files that are named with the `.pkr.hcl`
file extension. There is also [a JSON-based variant of the
language](/docs/templates/hcl_templates/syntax-json) that is named with the `.pkr.json` file
2019-12-18 10:57:36 -05:00
extension.
Configuration files must always use UTF-8 encoding, and by convention are
usually maintained with Unix-style line endings (LF) rather than Windows-style
line endings (CRLF), though both are accepted.
## Configuration Ordering
The ordering of root blocks is not significant. The order of `provisioner` or
`post-processor` blocks within a `build` is the only major feature where block
order matters.