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

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