Writing Packer Templates: Difference between revisions
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=External= | |||
* https://learn.hashicorp.com/tutorials/packer/docker-get-started-build-image#write-packer-template | |||
* https://www.packer.io/docs/templates/engine.html | |||
=Internal= | =Internal= | ||
* [[Packer_Concepts#Writing_Templates|Packer Concepts]] | * [[Packer_Concepts#Writing_Templates|Packer Concepts]] | ||
=Template Structure= | |||
==HCL Syntax== | |||
<syntaxhighlight lang='groovy'> | |||
packer { | |||
} | |||
source { | |||
} | |||
build { | |||
} | |||
</syntaxhighlight> | |||
===Packer Block=== | |||
The <code>packer{}</code> block contains Packer settings, including the Packer version. | |||
===Source Block=== | |||
The <code>source{}</code> configures a specific builder plugin, which is then invoked by the [[#Build_Block|build block]]. | |||
===Build Block=== | |||
=Template User Variables= | =Template User Variables= | ||
{{External|https://www.packer.io/docs/templates/user-variables.html}} | {{External|https://www.packer.io/docs/templates/user-variables.html}} | ||
=Template Variables= | |||
{{External|https://www.packer.io/docs/templates/engine.html#template-variables}} | |||
Template variables are special variables automatically set by Packer at build time. Some builders, provisioners and other components have template variables that are available only for that component. Template variables are recognizable because they're prefixed by a period, such as <code>{{ .Name }}</code>. For example, when using the shell provisioner template variables are available to customize the <code>execute_command</code> parameter used to determine how Packer will run the shell command: | |||
<syntaxhighlight lang='json'> | |||
{ | |||
"provisioners": [ | |||
{ | |||
"type": "shell", | |||
"execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}}'", | |||
"scripts": [ | |||
"scripts/bootstrap.sh" | |||
] | |||
} | |||
] | |||
} | |||
</syntaxhighlight> | |||
==.Vars== | |||
The <code>{{ .Vars }}</code> template variable is replaced with the list of environment variables, if configured as <code>environment_vars</code>. | |||
==.Path== | |||
The <code>{{ .Path }}</code> template variable is replaced with the path of the script to be executed. | |||
==.EnvVarFile== | |||
=Examples= | =Examples= |
Latest revision as of 02:09, 4 June 2021
External
- https://learn.hashicorp.com/tutorials/packer/docker-get-started-build-image#write-packer-template
- https://www.packer.io/docs/templates/engine.html
Internal
Template Structure
HCL Syntax
packer {
}
source {
}
build {
}
Packer Block
The packer{}
block contains Packer settings, including the Packer version.
Source Block
The source{}
configures a specific builder plugin, which is then invoked by the build block.
Build Block
Template User Variables
Template Variables
Template variables are special variables automatically set by Packer at build time. Some builders, provisioners and other components have template variables that are available only for that component. Template variables are recognizable because they're prefixed by a period, such as {{ .Name }}
. For example, when using the shell provisioner template variables are available to customize the execute_command
parameter used to determine how Packer will run the shell command:
{
"provisioners": [
{
"type": "shell",
"execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}}'",
"scripts": [
"scripts/bootstrap.sh"
]
}
]
}
.Vars
The {{ .Vars }}
template variable is replaced with the list of environment variables, if configured as environment_vars
.
.Path
The {{ .Path }}
template variable is replaced with the path of the script to be executed.
.EnvVarFile
Examples
Amazon EBS Template Example
A simple template that produces an Amazon AMI
{
}