github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/hcl2/locals.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: Local Values - HCL Configuration Language
     4  description: >-
     5    Local values assign a name to an expression that can then be used multiple
     6    times within a folder.
     7  ---
     8  
     9  # Local Values
    10  
    11  Local values assign a name to an expression, that can then be used multiple
    12  times within a folder.
    13  
    14  If [variables](/docs/job-specification/hcl2/variables) are analogous to
    15  function arguments then _local values_ are comparable to a function's local
    16  variables.
    17  
    18  ## Examples
    19  
    20  Local values are defined in `locals` blocks:
    21  
    22  ```hcl
    23  # A computed default name prefix
    24  locals {
    25    default_name_prefix = "${var.project_name}-web"
    26    name_prefix         = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}"
    27  
    28    # unlike variables, locals don't have type constraints, so if you use
    29    # functions that take maps but not objects, you may need to convert them
    30    number_of_ports = length(convert({"www" = "80"}, map(string)))
    31  }
    32  
    33  # Local values can be interpolated elsewhere using the "local." prefix.
    34  job "example_loadbalancer" {
    35    name = "${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.