github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/docs/job-specification/hcl2/locals.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: Local Values - HCL Configuration Language
     4  sidebar_title: Locals
     5  description: >-
     6    Local values assign a name to an expression that can then be used multiple
     7    times within a folder.
     8  ---
     9  
    10  # Local Values
    11  
    12  Local values assign a name to an expression, that can then be used multiple
    13  times within a folder.
    14  
    15  If [variables](/docs/job-specification/hcl2/variables) are analogous to
    16  function arguments then _local values_ are comparable to a function's local
    17  variables.
    18  
    19  ## Examples
    20  
    21  Local values are defined in `locals` blocks:
    22  
    23  ```hcl
    24  # A computed default name prefix
    25  locals {
    26    default_name_prefix = "${var.project_name}-web"
    27    name_prefix         = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}"
    28  
    29    # unlike variables, locals don't have type constraints, so if you use
    30    # functions that take maps but not objects, you may need to convert them
    31    number_of_ports = length(convert({"www" = "80"}, map(string)))
    32  }
    33  
    34  # Local values can be interpolated elsewhere using the "local." prefix.
    35  job "${local.name_prefix}_loadbalancer" {
    36    # ...
    37  }
    38  ```
    39  
    40  ## Description
    41  
    42  The `locals` block defines one or more local variables.
    43  
    44  The names given for the items in the `locals` block must be unique. The given
    45  value can be any expression that is valid within the current file.
    46  
    47  The expression of a local value can refer to other locals, but reference cycles
    48  are not allowed. That is, a local cannot refer to itself or to a variable that
    49  refers (directly or indirectly) back to it.
    50  
    51  It's recommended to group together logically-related local values into a single
    52  block, particularly if they depend on each other. This will help the reader
    53  understand the relationships between variables. Conversely, prefer to define
    54  _unrelated_ local values in _separate_ blocks, and consider annotating each
    55  block with a comment describing any context common to all of the enclosed
    56  locals.