Add extend packer build template engine docs (#8615)

This commit is contained in:
Sylvia Moss 2020-01-17 11:21:52 +01:00 committed by GitHub
parent 76fa85fd93
commit fce24ca71d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 6 deletions

View File

@ -159,3 +159,44 @@ necessary.
and will likely change in a future version. They aren't fully "baked" yet, so
they aren't documented here other than to tell you how to hook in provisioners.
## Template Engine
### Build variables
Packer makes it possible to provide custom template engine variables to be shared with
provisioners using the `build` function.
Part of the builder interface changes made in 1.5.0 was to make builder Prepare() methods
return a list of custom variables which we call `generated data`.
We use that list of variables to generate a custom placeholder map per builder that
combines custom variables with the placeholder map of default build variables created by Packer.
Here's an example snippet telling packer what will be made available by the builder:
```
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
// ...
generatedData := []string{"SourceImageName"}
return generatedData, warns, nil
}
```
Returning the custom variable name(s) within the `generated_data` placeholder is necessary
for the template containing the build variable(s) to validate.
Once the placeholder is set, it's necessary to pass the variables' real values when calling
the provisioner. This can be done as the example below:
```
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
// ...
// Create map of custom variable
generatedData := map[string]interface{}{"SourceImageName": "the source image name value"}
// Pass map to provisioner
hook.Run(context.Context, packer.HookProvision, ui, comm, generatedData)
// ...
}
```
To know more about the template engine build function, please refer to the [template engine docs](/docs/templates/engine.html).

View File

@ -64,14 +64,16 @@ Here is a full list of the available functions for reference.
variable](/docs/templates/user-variables.html#using-home-variable)
- `build` - This engine will allow you to access special variables that
provide connection information and basic instance state information.
Usage example:
Usage example:
```json
{
"type": "shell-local",
"environment_vars": ["TESTVAR={{ build `PackerRunUUID`}}"],
"inline": ["echo $TESTVAR"]
}
{
"type": "shell-local",
"environment_vars": ["TESTVAR={{ build `PackerRunUUID`}}"],
"inline": ["echo $TESTVAR"]
}
```
Valid variables to request are: "ID", "Host",
"Port", "User", "Password", "ConnType",
"PackerRunUUID", "SSHPublicKey", and "SSHPrivateKey".