yq/usage/hcl.md
2025-12-15 11:38:27 +11:00

3.4 KiB

HCL

Encode and decode to and from HashiCorp Configuration Language (HCL).

HCL is commonly used in HashiCorp tools like Terraform for configuration files. The yq HCL encoder and decoder support:

  • Blocks and attributes
  • String interpolation and expressions (preserved without quotes)
  • Comments (leading, head, and line comments)
  • Nested structures (maps and lists)
  • Syntax colorization when enabled

Parse HCL

Given a sample.hcl file of:

io_mode = "async"

then

yq -oy sample.hcl

will output

io_mode: "async"

Roundtrip: Sample Doc

Given a sample.hcl file of:

service "cat" {
  process "main" {
    command = ["/usr/local/bin/awesome-app", "server"]
  }

  process "management" {
    command = ["/usr/local/bin/awesome-app", "management"]
  }
}

then

yq sample.hcl

will output

service "cat" {
  process "main" {
    command = ["/usr/local/bin/awesome-app", "server"]
  }
  process "management" {
    command = ["/usr/local/bin/awesome-app", "management"]
  }
}

Roundtrip: With an update

Given a sample.hcl file of:

service "cat" {
  process "main" {
    command = ["/usr/local/bin/awesome-app", "server"]
  }

  process "management" {
    command = ["/usr/local/bin/awesome-app", "management"]
  }
}

then

yq '.service.cat.process.main.command += "meow"' sample.hcl

will output

service "cat" {
  process "main" {
    command = ["/usr/local/bin/awesome-app", "server", "meow"]
  }
  process "management" {
    command = ["/usr/local/bin/awesome-app", "management"]
  }
}

Parse HCL: Sample Doc

Given a sample.hcl file of:

service "cat" {
  process "main" {
    command = ["/usr/local/bin/awesome-app", "server"]
  }

  process "management" {
    command = ["/usr/local/bin/awesome-app", "management"]
  }
}

then

yq -oy sample.hcl

will output

service:
  cat:
    process:
      main:
        command:
          - "/usr/local/bin/awesome-app"
          - "server"
      management:
        command:
          - "/usr/local/bin/awesome-app"
          - "management"

Parse HCL: with comments

Given a sample.hcl file of:

# Configuration
port = 8080 # server port

then

yq -oy sample.hcl

will output

# Configuration
port: 8080 # server port

Roundtrip: with comments

Given a sample.hcl file of:

# Configuration
port = 8080

then

yq sample.hcl

will output

# Configuration
port = 8080

Roundtrip: With templates, functions and arithmetic

Given a sample.hcl file of:

# Arithmetic with literals and application-provided variables
sum = 1 + addend

# String interpolation and templates
message = "Hello, ${name}!"

# Application-provided functions
shouty_message = upper(message)

then

yq sample.hcl

will output

# Arithmetic with literals and application-provided variables
sum = 1 + addend
# String interpolation and templates
message = "Hello, ${name}!"
# Application-provided functions
shouty_message = upper(message)

Roundtrip: Separate blocks with same name.

Given a sample.hcl file of:

resource "aws_instance" "web" {
  ami = "ami-12345"
}
resource "aws_instance" "db" {
  ami = "ami-67890"
}

then

yq sample.hcl

will output

resource "aws_instance" "web" {
  ami = "ami-12345"
}
resource "aws_instance" "db" {
  ami = "ami-67890"
}