Add extend packer build template engine docs (#8615)
This commit is contained in:
parent
76fa85fd93
commit
fce24ca71d
|
@ -159,3 +159,44 @@ necessary.
|
||||||
and will likely change in a future version. They aren't fully "baked" yet, so
|
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.
|
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).
|
|
@ -65,6 +65,7 @@ Here is a full list of the available functions for reference.
|
||||||
- `build` - This engine will allow you to access special variables that
|
- `build` - This engine will allow you to access special variables that
|
||||||
provide connection information and basic instance state information.
|
provide connection information and basic instance state information.
|
||||||
Usage example:
|
Usage example:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"type": "shell-local",
|
"type": "shell-local",
|
||||||
|
@ -72,6 +73,7 @@ Here is a full list of the available functions for reference.
|
||||||
"inline": ["echo $TESTVAR"]
|
"inline": ["echo $TESTVAR"]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Valid variables to request are: "ID", "Host",
|
Valid variables to request are: "ID", "Host",
|
||||||
"Port", "User", "Password", "ConnType",
|
"Port", "User", "Password", "ConnType",
|
||||||
"PackerRunUUID", "SSHPublicKey", and "SSHPrivateKey".
|
"PackerRunUUID", "SSHPublicKey", and "SSHPrivateKey".
|
||||||
|
|
Loading…
Reference in New Issue