mirror of https://github.com/hashicorp/packer
Add consul_key function to integrate concul with hcl2 tempaltes. (#10119)
* Add consul_key function to integrate concul with hcl2 tempaltes. * sidebar navpull/10128/head
parent
4f5e878a17
commit
cc7dbf6092
@ -0,0 +1,25 @@
|
||||
package function
|
||||
|
||||
import (
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
|
||||
commontpl "github.com/hashicorp/packer/common/template"
|
||||
)
|
||||
|
||||
// ConsulFunc constructs a function that retrieves KV secrets from HC vault
|
||||
var ConsulFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "key",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
key := args[0].AsString()
|
||||
val, err := commontpl.Consul(key)
|
||||
|
||||
return cty.StringVal(val), err
|
||||
},
|
||||
})
|
||||
@ -0,0 +1,42 @@
|
||||
---
|
||||
layout: docs
|
||||
page_title: consul - Functions - Configuration Language
|
||||
sidebar_title: consul
|
||||
description: The consul function retrieves secrets from HashiCorp consul KV stores.
|
||||
---
|
||||
|
||||
|
||||
# `consul_key` Function
|
||||
|
||||
[Consul](https://www.consul.io) keys can be used within your template using the
|
||||
`consul_key` function.
|
||||
|
||||
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](https://www.consul.io/docs/commands#environment-variables).
|
||||
Loading…
Reference in new issue