mirror of
https://github.com/hashicorp/packer.git
synced 2026-06-09 08:42:33 -04:00
clarify local and input variables (#12334)
* clarify local and input variables * Apply suggestions from code review Co-authored-by: Rose M Koron <32436232+rkoron007@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Rose M Koron <32436232+rkoron007@users.noreply.github.com> Co-authored-by: Wilken Rivera <wilken@hashicorp.com> * Apply suggestions from code review Co-authored-by: Wilken Rivera <wilken@hashicorp.com> --------- Co-authored-by: Rose M Koron <32436232+rkoron007@users.noreply.github.com> Co-authored-by: Wilken Rivera <wilken@hashicorp.com>
This commit is contained in:
parent
e0a5286645
commit
485e080725
2 changed files with 18 additions and 21 deletions
|
|
@ -1,35 +1,34 @@
|
|||
---
|
||||
page_title: Local Values - HCL Configuration Language
|
||||
page_title: Local Variables - HCL Configuration Language
|
||||
description: >-
|
||||
Local values assign a name to an expression that can then be used multiple
|
||||
Local variables assign a name to an expression that you can use multiple
|
||||
times within a folder.
|
||||
---
|
||||
|
||||
# Local Values
|
||||
# Local Variables
|
||||
|
||||
`@include 'from-1.5/beta-hcl2-note.mdx'`
|
||||
|
||||
There are two kinds of variables in HCL Packer templates: Input variables,
|
||||
sometimes simply called "variables", and Local variables, also known as
|
||||
"locals". Input variables may have defaults, but those defaults can
|
||||
be overridden from the command line or special variable files. Local variables
|
||||
can be thought of as constants, and are not able to be overridden at runtime.
|
||||
"locals". Input variables may have defaults, but those defaults can be
|
||||
overridden using command line options, environment variables, or variable
|
||||
definitions files. Local variables can not be overridden.
|
||||
|
||||
This page is about local variables. To learn about input variables, see the
|
||||
[input variables](/packer/docs/templates/hcl_templates/variables) page.
|
||||
|
||||
Local values assign a name to an expression, that can then be used multiple
|
||||
times within a folder.
|
||||
|
||||
If [variables](/packer/docs/templates/hcl_templates/variables) are analogous to function arguments then
|
||||
_local values_ are comparable to a function's local variables.
|
||||
Local variables assign a name to an expression, which you can use multiple
|
||||
times within a folder. The expression is evaluated at run time, and can
|
||||
reference input variables, other local variables, data sources, and HCL
|
||||
functions.
|
||||
|
||||
Input variable and local variable usage are introduced in the [_Variables
|
||||
Guide_](/packer/guides/hcl/variables).
|
||||
|
||||
## Examples
|
||||
|
||||
Local values are defined in `local` or `locals` blocks:
|
||||
Local variables are defined in a `local` or `locals` block:
|
||||
|
||||
```hcl
|
||||
# Using the local block allows you to mark locals as sensitive, which will
|
||||
|
|
@ -51,7 +50,7 @@ locals {
|
|||
name_prefix = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}"
|
||||
}
|
||||
|
||||
# Local values can be interpolated elsewhere using the "local." prefix.
|
||||
# Local variables can be referenced using the "local." prefix.
|
||||
source "virtualbox-iso" "example" {
|
||||
output = "${local.name_prefix}-files"
|
||||
# ...
|
||||
|
|
@ -120,10 +119,10 @@ The expression of a local value can refer to other locals, but reference cycles
|
|||
are not allowed. That is, a local cannot refer to itself or to a variable that
|
||||
refers (directly or indirectly) back to it.
|
||||
|
||||
It's recommended to group together logically-related local values into a single
|
||||
It's recommended to group together logically-related local variables into a single
|
||||
block, particularly if they depend on each other. This will help the reader
|
||||
understand the relationships between variables. Conversely, prefer to define
|
||||
_unrelated_ local values in _separate_ blocks, and consider annotating each
|
||||
_unrelated_ local variables in _separate_ blocks, and consider annotating each
|
||||
block with a comment describing any context common to all of the enclosed
|
||||
locals.
|
||||
|
||||
|
|
|
|||
|
|
@ -11,9 +11,10 @@ description: |-
|
|||
|
||||
There are two kinds of variables in HCL Packer templates: Input variables,
|
||||
sometimes simply called "variables", and Local variables, also known as
|
||||
"locals". Input variables may have defaults, but those defaults can
|
||||
be overridden from the command line or special variable files. Local variables
|
||||
can be thought of as constants, and are not able to be overridden at runtime.
|
||||
"locals". Input variables may have defaults, but those defaults can be
|
||||
overridden using command line options, environment variables, or variable
|
||||
definitions files. However, nothing can change the value of an input variable
|
||||
after the initial override.
|
||||
|
||||
This page is about input variables. To learn about local variables, see the
|
||||
[locals](/packer/docs/templates/hcl_templates/locals) page.
|
||||
|
|
@ -21,9 +22,6 @@ This page is about input variables. To learn about local variables, see the
|
|||
Input variables serve as parameters for a Packer build, allowing aspects of the
|
||||
build to be customized without altering the build's own source code.
|
||||
|
||||
When you declare variables in the build of your configuration, you can set
|
||||
their values using CLI options and environment variables.
|
||||
|
||||
Input variable and local variable usage are introduced in the [_Variables
|
||||
Guide_](/packer/guides/hcl/variables).
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue