How to use Templates with Consul

Chưa phân loại
Consul Template is a simple and powerful tool that populates values from consul into the file system. Consul templates can also execute some commands. Generally, consul template run as a daemon, and this daemon can queries a consul cluster and updates any number of specified templates on the filesystem. The consul template is widely used for updating service configuration files.


  1. A Virtual instance server
  2. A root user
  3. Consul installed on the server

Update System

We recommend you to upgrade all the packages and repositories before installing any new package on the system. Execute the following command and it will do the job for you.

apt-get update

Install Consul Template

In this guide, we assume that you have already installed consul on the server and also created a consul cluster. Now we will be installing and using the consul template but consul agent must be running for using the consul template. The consul template is not by default added in the consul server package, we will need to download it and install it separately. To do so, you will need to download the latest consul template binary from it’s Official download webpage. Execute the following command and it will do the job for you.

curl -o

Next, you will need to extract the above-downloaded archive using the following command.

tar -zxf consul-template_0.20.0_linux_amd64.tgz

You can also install it locally using docker if you want. Once you have the docker installed on your system, you can clone the official consul template repository using the following command.

git clone

Next, execute the following command to compile the consul template binary. make dev Now you have successfully installed the consul template on your server.

Configuring Consul Template

We have consul template installed on your system now we are ready to use it. Here is a sample configuration file for consul-template ctemplate config.hcl.

consul {
address = "locahost:8500"
retry {
enabled = true
attempts = 12
backoff = "250ms"
token = "w94RIMKUtQH1a4VJGN+t+vn1Y0nErc/ch93E1F1ZcHU="
reload_signal = "SIGHUP"kill_signal = "SIGINT"max_stale = "10m"log_level = "warn"#
pid_file = "/consul-template/"wait {
min = "5s"
max = "10s"
vault {
address = "

token = "R/Uf0tYa5YkhPLpNLL807KWJ4ZiJi3clyQEfaMoRSJg"
renew_token = false
deduplicate {
enabled = true
# prefix = "consul-template/dedup/"
template {
source      = "./vault/templates/pki/cert.ctmpl"
destination = "./vault/output/pki/"
perms = 0400
left_delimiter  = "{{"
right_delimiter = "}}"
wait {
min = "2s"
max = "10s"
template {
source      = "./vault/templates/pki/ca.ctmpl"
destination = "./vault/output/pki/"
template {
source      = "./vault/templates/pki/key.ctmpl"
destination = "./vault/output/pki/"

In the above configuration file, please change the consul address, vault address, consul token, vault token, source template paths and output file paths with your actual values. Now you can run the consul template using the following command.

./consul-template -config config.hcl

Creating Certificates dynamically with Vault

Vault is a widely known open source tool for managing secret data. Here, we will see another use of the consul template for creating certificates dynamically with vault. Consul template can run more than one template. In order to create certificates programatically, you will need these three templates given below.

  1. ca.ctmpl
{{/* ./vault/templates/ca.ctmpl */}}
{{ with secret "pki-int/issue/cert-generator" "" }}
{{ .Data.issuing_ca }}{{ end }}
  1. ctmpl
{{/* ./vault/templates/cert.ctmpl */}}
{{ with secret "pki-int/issue/cert-generator" "" }}
{{ .Data.certificate }}{{ end }}
  1. ctmpl
{{/* ./vault/templates/key.ctmpl */}}
{{ with secret "pki-int/issue/cert-generator" "" }}
{{ .Data.private_key }}{{ end }}

Please don’t forget to replace com with your actual domain. The above three templates are three different input templates but they will be compressed into a single API call when they run under the same consul template process. Now when you have all the above consul template and configuration ready then you can use it to create certificates dynamically using the following command.

./consul-template -config config.hcl

You can also use the consul template to discover all the services running in your consul cluster. To do so, you will need to create a template saved as all-services.tpl.

{{range services}}# {{.Name}}{{range service .Name}}

Once you have created the template now you will need to run the template. This time we will just specify the template file to run the template. Execute the following command to run the template.

consul-template -template="all-services.tpl:all-services.txt" -once

We have used a flag – once in the above command to run the process once and then it will automatically quit. You will see the following output for the running services:

# consul

# redis

# web


In this guide, you have learned how to install and set up a consul template on your server. Now you can use the consul template to perform various operations on your applications. We hope now you have enough knowledge to work with consul template.

Sandclock IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, Sandclock IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Top 5 Lightweight Web Browsers for Linux

Various Linux distributions provide a number of lightweight browsers that can easily run without eating up too much of...

Linux File Managers and NextCloud Integration

In this article, I am going to show you how to integrate your NextCloud account with your Linux desktop and access NextCloud...

Setup NAS Server on Raspberry Pi 3

NAS servers are really costly these days. If you don’t have that much money to spare, then you use Raspberry Pi 3 to set...
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019

How to Update Ubuntu Linux

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu

Hướng dẫn cài đặt Apache CloudStack

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader