github.com/KyaXTeam/consul@v1.4.5/website/source/docs/guides/consul-template.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Consul Template"
     4  sidebar_current: "docs-guides-consul-template"
     5  description: |-
     6    Consul template provides a programmatic method for rendering configuration files from Consul data.
     7  ---
     8  
     9  # Consul Template
    10  
    11  The Consul template tool provides a programmatic method
    12  for rendering configuration files from a variety of locations,
    13  including Consul KV. It is an ideal option for replacing complicated API
    14  queries that often require custom formatting.
    15  The template tool is based on Go templates and shares many
    16  of the same attributes.
    17  
    18  Consul template is a useful tool with several uses, we will focus on two
    19  of it's use cases.
    20  
    21  1. *Update configuration files*. The Consul template tool can be used
    22  to update service configuration files. A common use case is managing load
    23  balancer configuration files that need to be updated regularly in a dynamic
    24  infrastructure on machines many not be able to directly connect to the Consul cluster.
    25  
    26  1. *Discover data about the Consul cluster and service*. It is possible to collect
    27  information about the services in your Consul cluster. For example, you could
    28  collect a list of all services running on the cluster or you could discover all
    29  service addresses for the Redis service. Note, this use case has limited
    30  scope for production.
    31  
    32  In this guide we will briefly discuss how `consul-template` works,
    33  how to install it, and two use cases.
    34  
    35  Before completing this guide, we assume some familiarity with
    36  [Consul KV](https://learn.hashicorp.com/consul/getting-started/kv)
    37   and [Go templates](https://golang.org/pkg/text/template/).
    38  
    39  ## Introduction to Consul Template
    40  
    41  Consul template is a simple, yet powerful tool. When initiated, it
    42  reads one or more template files and queries Consul for all
    43  data needed to render them. Typically, you run `consul-template` as a
    44  daemon which will fetch the initial values and then continue to watch
    45  for updates, re-rendering the template whenever there are relevant changes in
    46  the cluster. You can alternatively use the `-once` flag to fetch and render
    47  the template once which is useful for testing and
    48  setup scripts that are triggered by some other automation for example a
    49  provisioning tool. Finally, the template can also run arbitrary commands after the update
    50  process completes. For example, it can send the HUP signal to the
    51  load balancer service after a configuration change has been made.
    52  
    53  The Consul template tool is flexible, it can fit into many
    54  different environments and workflows. Depending on the use-case, you
    55  may have a single `consul-template` instance on a handful of hosts
    56  or may need to run several instances on every host. Each `consul-template`
    57  process can manage multiple unrelated files though and will de-duplicate
    58   the fetches as needed if those files share data dependencies so it can
    59  reduce the load on Consul servers to share where possible.
    60  
    61  ## Install Consul Template
    62  
    63  For this guide, we are using a local Consul agent in development
    64  mode which can be started with `consul agent -dev`. To quickly set
    65  up a local Consul agent, refer to the getting started [guide](https://learn.hashicorp.com/consul/getting-started/install). The
    66  Consul agent must be running to complete all of the following
    67  steps.
    68  
    69  The Consul template tool is not included with the Consul binary and will
    70  need to be installed separately. It can be installed from a precompiled
    71  binary or compiled from source. We will be installing the precompiled binary.
    72  
    73  First, download the binary from the [Consul Template releases page](https://releases.hashicorp.com/consul-template/).
    74  
    75  ```sh
    76  curl -O https://releases.hashicorp.com/consul-template/0.19.5/consul-template<_version_OS>.tgz
    77  ```
    78  
    79  Next, extract the binary and move it into your `$PATH`.
    80  
    81  ```sh
    82  tar -zxf consul-template<_version_OS>.tgz
    83  ```
    84  
    85  To compile from source, please see the instructions in the
    86  [contributing section in GitHub](https://github.com/hashicorp/consul-template#contributing).
    87  
    88  ## Use Case: Consul KV
    89  
    90  In this first use case example, we will render a template that pulls the HashiCorp address
    91  from Consul KV. To do this we will create a simple template that contains the HashiCorp
    92  address, run `consul-template`, add a value to Consul KV for HashiCorp's address, and
    93  finally view the rendered file.
    94  
    95  First, we will need to create a template file `find_address.tpl` to query
    96  Consul's KV store:
    97  
    98  ```liquid
    99  {{ key "/hashicorp/street_address" }}
   100  ```
   101  
   102  Next, we will run `consul-template` specifying both
   103  the template to use and the file to update.
   104  
   105  ```shell
   106  $ consul-template -template "find_address.tpl:hashicorp_address.txt"
   107  ```
   108  
   109  The `consul-template` process will continue to run until you kill it with `CRTL+c`.
   110  For now, we will leave it running.
   111  
   112  Finally, open a new terminal so we can write data to the key in Consul using the command
   113  line interface.
   114  
   115  ```shell
   116  $ consul kv put hashicorp/street_address "101 2nd St"
   117  
   118  Success! Data written to: hashicorp/street_address
   119  ```
   120  
   121  We can ensure the data was written by viewing the `hashicorp_address.txt`
   122  file which will be located in the same directory where `consul-template`
   123  was run.
   124  
   125  ```shell
   126  $ cat hashicorp_address.txt
   127  
   128  101 2nd St
   129  ```
   130  
   131  If you update the key `hashicorp/street_address`, you can see the changes to the file
   132  immediately. Go ahead and try `consul kv put hashicorp/street_address "22b Baker ST"`.
   133  
   134  You can see that this simple process can have powerful implications. For example, it is
   135  possible to use this same process for updating your [HAProxy load balancer
   136  configuration](https://github.com/hashicorp/consul-template/blob/master/examples/haproxy.md).
   137  
   138  You can now kill the `consul-template` process with `CTRL+c`.
   139  
   140  ## Use Case: Discover All Services
   141  
   142  In this use case example, we will discover all the services running in the Consul cluster.
   143  To follow along, you use the local development agent from the previous example.
   144  
   145  First, we will need to create a new template `all-services.tpl` to query all services.
   146  
   147  ```liquid
   148  {{range services}}# {{.Name}}{{range service .Name}}
   149  {{.Address}}{{end}}
   150  
   151  {{end}}
   152  ```
   153  
   154  Next, run Consul template specifying the template we just created and the `-once` flag.
   155  The `-once` flag will tell the process to run once and then quit.
   156  
   157  ```shell
   158  $ consul-template -template="all-services.tpl:all-services.txt" -once
   159  ```
   160  
   161  If you complete this on your local development agent, you should
   162  still see the `consul` service when viewing `all-services.txt`.
   163  
   164  ```text
   165  # consul
   166  127.0.0.7
   167  ```
   168  On a development or production cluster, you would see a list of all the services.
   169  For example:
   170  
   171  ```text
   172  # consul
   173  104.131.121.232
   174  
   175  # redis
   176  104.131.86.92
   177  104.131.109.224
   178  104.131.59.59
   179  
   180  # web
   181  104.131.86.92
   182  104.131.109.224
   183  104.131.59.59
   184  ```
   185  
   186  ## Conclusion
   187  
   188  In this guide we learned how to set up and use the Consul template tool.
   189  To see additional examples, refer to the examples folder
   190  in [GitHub](https://github.com/hashicorp/consul-template/tree/master/examples).