HashiCorp Certified: Consul Associate Certification
Access the Consul KeyValue KV
Using consul template
Consul Template is a HashiCorp tool designed to render configuration files dynamically using data from a Consul cluster. Running as a standalone daemon alongside a Consul agent, it continuously watches key/value entries and regenerates files when values change. This ensures seamless configuration updates without manual restarts.
Workflow
- Define a template file with placeholders (e.g.,
app.ctmpl
). - Start the Consul agent on your server:
consul agent -config-dir=/etc/consul.d
- Launch the
consul-template
process, specifying the template and target:consul-template \ -template "/etc/templates/app.ctmpl:/etc/app/config.yaml" \ -config /etc/consul-template/config.hcl
- The daemon retrieves key/value pairs from Consul.
- Placeholders in the template are replaced with live data.
- The rendered file is saved to disk.
- An optional
command
(like a service reload) is executed to apply updates.
Note
By default, Consul Template polls Consul every two seconds. Adjust the interval with the -wait
flag or in HCL.
Key Benefits
Benefit | Description |
---|---|
Dynamic Updates | Automatically re-renders files when Consul KV entries change. |
Automatic Reloads | Executes custom commands (e.g., systemctl reload ) on changes. |
Powerful Templating | Leverages Go templates with conditionals, loops, and functions. |
Example HCL Configuration
Create /etc/consul-template/config.hcl
:
template {
source = "/etc/templates/app.ctmpl"
destination = "/etc/app/config.yaml"
command = "systemctl reload my-app.service"
left_delimiter = "{{"
right_delimiter = "}}"
}
Run the daemon:
consul-template -config /etc/consul-template/config.hcl
Warning
Ensure the Consul agent is running and reachable. When ACLs are enabled, include the token
parameter in your HCL or CLI invocation.
Links and References
Watch Video
Watch video content