Back to Packer

`consul_key` Function

website/content/docs/templates/hcl_templates/functions/contextual/consul.mdx

1.15.31.6 KB
Original Source

⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️

[!IMPORTANT]
Documentation Update: Product documentation previously located in /website has moved to the hashicorp/web-unified-docs repository, where all product documentation is now centralized. Please make contributions directly to web-unified-docs, since changes to /website in this repository will not appear on developer.hashicorp.com. ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️

consul_key Function

Retrieves secrets from a HashiCorp Consul KV store. You can use the consul_key function secrets to add them to your template. Refer to the Consul documentation for additional information about the Consul KV.

You can either use this function in a locals block or directly inline where you want to use the value.

hcl
locals {
  my_version = "${consul_key("myservice/version")}"
}

source "null" "first-example" {
  communicator = "none"
}

build {
  name = "my-build-name"
  sources = ["null.first-example"]

  provisioner "shell-local" {
    environment_vars = ["TESTVAR=${build.PackerRunUUID}"]
    inline = ["echo my_version is '${local.my_version}'",
              "echo version is '${consul_key("myservice/version")}'."]
  }
}

This will load the key stored at the path myservice/version from consul.

The configuration for consul (address, tokens, ...) must be specified as environment variables, as specified in the Documentation.