https://github.com/jameswoolfenden/consul-template-by-example
consul-template-by-example
https://github.com/jameswoolfenden/consul-template-by-example
consul consul-template development
Last synced: about 2 months ago
JSON representation
consul-template-by-example
- Host: GitHub
- URL: https://github.com/jameswoolfenden/consul-template-by-example
- Owner: JamesWoolfenden
- License: apache-2.0
- Created: 2018-04-02T17:35:26.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-11T22:00:16.000Z (over 7 years ago)
- Last Synced: 2025-01-25T19:28:20.321Z (over 1 year ago)
- Topics: consul, consul-template, development
- Language: Shell
- Size: 10.7 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Consul template: By Example
This is a very simple example shows how to use consul and consul-template to get data in and out, and how to handle missing and default values. The consul setup is only for development purposes.
Download this repository [here](https://github.com/JamesWoolfenden/consul-template-by-example)
## Set-up the tools
Run both shell scripts (_setup.sh_ and _setup-consul.sh_) to get the tools consul and consul-template. The install examples assume linux but this could be any supported platform as long as the tools are installed in your path.
## Start/Spawn Consul
`consul agent -dev &`
There's an example template file called _miss.tpl_. This looks like:
```jinja2
Key:Foo:{{key "foo"}}
MissingKey:{{if keyExists "MissingKey"}}{{key "MissingKey"}}{{else}}nothing{{end}}
DefaultValue:{{keyOrDefault "Duffer" ""}}
```
Consul-template uses the Go templating format- curly brackets the rest of the file is treated as text. The first line is a regular KV retrieval, the second line shows how to cope with missing values and the last line for default values.
First we'll add some data to Consul.
`consul kv put foo bar`
Then when we run consul-template:
`consul-template -template miss.tpl:miss.out -once`
The -once option "Do not run the process as a daemon".
Check the contents of the **miss.out** file.
```jina2
Key:Foo:bar
MissingKey:nothing
DefaultValue:
```
Then we'll add a value for _MissingKey_ variable.
`consul kv put MissingKey Time`
and retry:
`consul-template -template miss.tpl:miss.out -once`
**miss.out** now becomes:
```jinja2
Key:Foo:bar
MissingKey:Time
DefaultValue:
```
So there you have it a pretty basic example for using some of the basics behind consul and consul-template as a KV store.