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

     1  ---
     2  layout: docs
     3  page_title: Configuration Language
     4  description: |-
     5    Noamd uses text files to describe infrastructure and to set variables.
     6    These text files are called Nomad job specifications and are
     7    written in the HCL language.
     8  ---
     9  
    10  # HCL2
    11  
    12  Nomad uses the Hashicorp Configuration Language - HCL - designed to allow
    13  concise descriptions of the required steps to get to a job file.
    14  Nomad 1.0 adopts
    15  [HCL2](https://github.com/hashicorp/hcl/blob/hcl2/README.md), the second
    16  generation of HashiCorp Configuration Language. HCL2 extends the HCL language by
    17  adding expressions and input variables support to improve job spec
    18  reusability and readability. Also, the new HCL2 parser improves the error
    19  messages for invalid jobs.
    20  
    21  ## HCL Parsing Context
    22  
    23  The [Nomad API uses JSON][jobs-api], not HCL, to represent Nomad jobs.
    24  When running commands like `nomad job run` and `nomad job plan`, the Nomad CLI
    25  parses HCL and ultimately converts it to JSON. Because this parsing happens locally
    26  (i.e., where the operator is running the CLI) before job submission, there are some
    27  limits to the capabilities that can be accessed by HCL job specifications. For
    28  example, scheduling information is not yet available, including information about
    29  the client. Similarly, HCL features that depend on external context will take that
    30  context from the local environment of the CLI (e.g., files, environment variables).
    31  
    32  [jobs-api]: /api-docs/jobs
    33  
    34  ## JSON Jobs
    35  
    36  Since HCL is a superset of JSON, `nomad job run example.json` will attempt to
    37  parse a JSON job using the HCL parser. However, the JSON format accepted by
    38  the HCL parser is not the same as the [API's JSON format][json-jobs-api]. The 
    39  HCL parser's JSON format is unspecified, so the API format is preferred. You can 
    40  use the API format with the [`-json` command line flag][run-json]:
    41  
    42  ```shell-session
    43  $ # Generate an HCL formatted job
    44  $ nomad init
    45  
    46  $ # Convert HCL to API JSON format
    47  $ nomad job run -output example.nomad > example.json
    48  
    49  $ # Submit with the -json flag
    50  $ nomad job run -json example.json
    51  ```
    52  
    53  
    54  [json-jobs-api]: /api-docs/json-jobs
    55  [run-json]: /docs/commands/job/run#json
    56  
    57  ## Arguments, Blocks, and Expressions
    58  
    59  The syntax of the HCL language consists of only a few basic elements:
    60  
    61  ```hcl
    62  task "example" {
    63    driver = "docker"
    64  }
    65  
    66  <BLOCK TYPE> "<BLOCK LABEL>" {
    67    # Block body
    68    <IDENTIFIER> = <EXPRESSION> # Argument
    69  }
    70  ```
    71  
    72  - _Blocks_ are containers for other content and usually represent the
    73    configuration of some kind of object, like a task. Blocks have a
    74    _block type,_ can have zero or more _labels,_ and have a _body_ that contains
    75    any number of arguments and nested blocks. Block labels must be string literals.
    76  - _Arguments_ assign a value to a name. They appear within blocks.
    77  - _Expressions_ represent a value, either literally or by referencing and
    78    combining other values. They appear as values for arguments, or within other
    79    expressions.
    80  
    81  For full details about Nomad's syntax, see:
    82  
    83  - [Configuration Syntax](/docs/job-specification/hcl2/syntax)
    84  - [Expressions](/docs/job-specification/hcl2/expressions)
    85  
    86  ## Backward Compatibilities
    87  
    88  HCL2 syntax closely mirrors HCL1, but has some minor changes. Most existing
    89  Nomad job specifications will not require any changes.
    90  
    91  When you run `nomad job run` or `nomad job plan`, the CLI will report any
    92  required changes. Also, you can activate a backwards compatibility mode by
    93  passing `-hcl1` to use Nomad's HCL1 parser instead.
    94  
    95  ### Blocks
    96  
    97  Nomad 0.12 and earlier allowed a few variations for defining blocks. For example, the following variations of `meta` were accepted:
    98  
    99  ```hcl
   100  meta {
   101    # meta attributes can be quoted or not
   102    "team"       = "..."
   103    organization = "..."
   104  }
   105  
   106  # meta can be an assignment to a map
   107  meta = { "team" = "...", organization = "..." }
   108  ```
   109  
   110  Starting with Nomad 1.0 and the HCL2 parser, only the block syntax with unquoted attributes is accepted:
   111  
   112  ```hcl
   113  meta {
   114    team         = "..."
   115    organization = "..."
   116  }
   117  ```
   118  
   119  Additionally, block attributes must be [HCL2 valid identifiers](https://github.com/hashicorp/hcl/blob/v2.8.0/hclsyntax/spec.md#identifiers).
   120  Generally, identifiers may only contain letters, numbers, underscore `_`,
   121  or a dash `-`, and start with a letter. Notable,
   122  [`meta`](/docs/job-specification/meta), and
   123  [`env`](/docs/job-specification/env) keys may not
   124  contain other symbols (e.g. `.`, `#`).
   125  
   126  Task driver config fields may require extra attention if they contain invalid
   127  identifiers. For example, docker [`sysctl`](/docs/drivers/docker#sysctl) must
   128  use the map assignment syntax if the keys aren't valid:
   129  
   130  ```hcl
   131  sysctl = {
   132    "net.core.somaxconn/docs/drivers/docker#sysctl" = "16384"
   133  }
   134  ```
   135  
   136  Additionally, task driver config fields may not nest block syntax within an
   137  assignment syntax. The following [`mounts`](/docs/drivers/docker#mounts) syntax is no longer valid:
   138  
   139  ```hcl
   140  # INVALID in Nomad 1.0
   141  mounts = [
   142    {
   143      type = "tmpfs"
   144      tmpfs_options {  # <- block syntax is not valid here
   145        size = 10000
   146      }
   147    }
   148  ]
   149  ```
   150  
   151  Here, the `tmpfs_options` block declaration is invalid HCL2 syntax, and must be an assignment instead:
   152  
   153  ```hcl
   154  # VALID in Nomad 1.0
   155  mounts = [
   156    {
   157      type = "tmpfs"
   158      tmpfs_options = {
   159        size = 10000
   160      }
   161    }
   162  ]
   163  ```
   164  
   165  Or better yet, the new [`mount`](/docs/drivers/docker#mount) syntax, introduced in Nomad 1.0.1, is more appropriate here:
   166  
   167  ```hcl
   168  mount {
   169    type = "tmpfs"
   170    tmpfs_options {
   171      size = 10000
   172    }
   173  }
   174  ```
   175  
   176  ### Multiline "here doc" string
   177  
   178  Nomad supports multi-line string literals in the so-called "heredoc" style, inspired by Unix shell languages:
   179  
   180  ```hcl
   181  template {
   182    data = <<EOF
   183  hello
   184    world
   185    EOF
   186  }
   187  ```
   188  
   189  HCL2 trims the whitespace preceding the delimiter in the last line. So in the
   190  above example, `data` is read as `"hello\n world\n "` in HCL1, but `"hello\n world\n"` (note lack of trailing whitespace) in HCL2.
   191  
   192  ### Decimals
   193  
   194  Nomad 0.12 and earlier accepted small decimal values without a leading zero
   195  (e.g. `.3`, `.59`, `.9`). In such case, Nomad 1.0 requires a leading zero (e.g.
   196  `0.3`, `0.59`, `0.9`).