• HashiCorp Developer

  • HashiCorp Cloud Platform
  • Terraform
  • Packer
  • Consul
  • Vault
  • Boundary
  • Nomad
  • Waypoint
  • Vagrant
Nomad
  • Install
  • Intro
  • Tutorials
  • Documentation
  • API
  • Tools
  • Plugins
  • Sign up
Nomad Home

Documentation

Skip to main contentOverview


    • Overview
      • Overview
      • Expressions
        • Overview
          • base64decode
          • base64encode
          • csvdecode
          • jsondecode
          • jsonencode
          • urlencode
          • yamldecode
          • yamlencode
      • Locals
      • Syntax
      • Variables
    • artifact
    • affinity
    • change_script
    • check
    • check_restart
    • connect
    • constraint
    • csi_plugin
    • device
    • dispatch_payload
    • env
    • ephemeral_disk
    • expose
    • gateway
    • group
    • job
    • lifecycle
    • logs
    • meta
    • migrate
    • multiregion
    • network
    • parameterized
    • periodic
    • proxy
    • reschedule
    • resources
    • restart
    • scaling
    • service
    • sidecar_service
    • sidecar_task
    • spread
    • task
    • template
    • update
    • upstreams
    • vault
    • volume
    • volume_mount
  • Schedulers

  • Nomad Ecosystem
  • Nomad Partnerships
  • Who Uses Nomad
  • FAQ

  • Resources

  • Tutorial Library
  • Community Forum
    (opens in new tab)
  • Support
    (opens in new tab)
  • GitHub
    (opens in new tab)
  1. Developer
  2. Nomad
  3. Documentation
  4. Job Specification
  5. HCL2
  6. Functions
  7. Encoding Functions
  8. yamlencode
  • Nomad
  • v1.3.x
  • v1.2.x
  • v1.1.x
  • v1.0.x
  • v0.12.x
  • v0.11.x

»yamlencode Function

yamlencode encodes a given value to a string using YAML 1.2 block syntax.

Warning: This function is currently experimental and its exact result format may change in future versions of Nomad, based on feedback. Do not use yamldecode to construct a value for any argument where changes to the result would be disruptive. To get a consistent string representation of a value use jsonencode instead; its results are also valid YAML because YAML is a JSON superset.

This function maps Nomad language values to YAML tags in the following way:

Nomad typeYAML type
string!!str
number!!float or !!int
bool!!bool
list(...)!!seq
set(...)!!seq
tuple(...)!!seq
map(...)!!map
object(...)!!map
Null value!!null

yamlencode uses the implied syntaxes for all of the above types, so it does not generate explicit YAML tags.

Because the YAML format cannot fully represent all of the Nomad language types, passing the yamlencode result to yamldecode will not produce an identical value, but the Nomad language automatic type conversion rules mean that this is rarely a problem in practice.

Examples

> yamlencode({"a":"b", "c":"d"})
"a": "b"
"c": "d"

> yamlencode({"foo":[1, 2, 3], "bar": "baz"})
"bar": "baz"
"foo":
- 1
- 2
- 3

> yamlencode({"foo":[1, {"a":"b","c":"d"}, 3], "bar": "baz"})
"bar": "baz"
"foo":
- 1
- "a": "b"
  "c": "d"
- 3

yamlencode always uses YAML's "block style" for mappings and sequences, unless the mapping or sequence is empty. To generate flow-style YAML, use jsonencode instead: YAML flow-style is a superset of JSON syntax.

Related Functions

  • jsonencode is a similar operation using JSON instead of YAML.
  • yamldecode performs the opposite operation, decoding a YAML string to obtain its represented value.
Edit this page on GitHub

On this page

  1. yamlencode Function
  2. Examples
  3. Related Functions
Give Feedback(opens in new tab)
  • Certifications
  • System Status
  • Terms of Use
  • Security
  • Privacy
  • Trademark Policy
  • Trade Controls
  • Give Feedback(opens in new tab)