JSON value: <%= JSON.parse( value_decoded ) %>
<%
end
%>
```
#### Fetch all values at once, but interrested only by a few
When using `kv('/my/multiple/values', recurse: true)`, only a single call is performed, thus,
it is far more efficient to retrive multiple values from the KV under the same root. Thus,
in order to display several discreet values, it is possible to do the following:
```erb
result = kv('/my/multiple/values', recurse: true)
value1 : <%= result.get_decoded('/my/multiple/values/value1') %>
value42 : <%= result.get_decoded('/my/multiple/values/value42') %>
value123 : <%= result.get_decoded('/my/multiple/values/value123') %>
```
Since `kv('/my/multiple/values', recurse: true)` will retrieve all values at once, it might be more
efficient in some cases than retrieving all values one by one.
## agent_members(wan: false, [agent: consul_agent_address])
[Get the Serf information](https://www.consul.io/api/agent.html#list-members) from Consul Agent point of view.
This is a list of Serf information containing serf information. This information is not consistent and should be used with care, most of the time, you should prefer `nodes()`.
If you are using `consul-templaterb` with a Consul server directly, you might use `wan:true` to have the list of all
consul servers connected thru WAN (aka all consul servers from all datacenters).
The returned value is an array containing the following objects containing the following attributes (accessed as Hash elements):
* "Name": Name of node
* "Addr": IP Address of node as seen in serf
* "Port": Serf port
* "Tags": Hash of properties, including version, dc, VSN info...
* "Status": Serf code from 0 to 5 giving Health information
Another property is available as `status()`, that translates the Hash property "Status" into something human redable ('none', 'alive', 'leaving', 'left', 'failed').
See [samples/members.json.erb](samples/members.json.erb) for example of usage.
## agent_metrics([agent: consul_agent_address])
[Get the metrics of Consul Agent](https://www.consul.io/api/agent.html#view-metrics). Since this endpoint does
not support blocking queries, data will be refreshed every few seconds, but will not use blocking queries
mechanism.
## agent_self([agent: consul_agent_address])
[Get the configuration of Consul Agent](https://www.consul.io/api/agent.html#read-configuration).
Since this endpoint does not support blocking queries, data will be refreshed every few seconds,
but will not use blocking queries mechanism.
## render_file(relative_path_to_erb_file, [params={}])
This allow to include a template file into another one. Beware, it does not check for infinite recursion!
The template can be either a static file either another template. The file has to be a valid template, but
can also be raw text (if it is a valid template) and is resolved with a relative path regarding the file
including it.
Example:
```erb
<%= render_file('common/header.html.erb', title: 'My Title') %>
```
Will render header.html.erb with parameter title = 'My Title'. `title` can then be accessed within
the template using `param('title', 'My default Value')` in the `header.html.erb` file.
## render_from_string(template_to_render, [params={}])
Similar to render_file but from a string.
Allows to render a template from a string. Useful if you have your templates in KV for instance.
Example:
Given the value in Consul's KV `my/template/to_render`: `from KV: <%= 5 * 2 %>`
```erb
<%= render_from_string(kv('my/template/to_render').get_value_decoded) %>
```
Would render the value: `from KV: 10`.
That's very usefull if you want to centralize your templates and being able to change the value
with a simple PUT call in the KV.
## param(parameter_name, [default_value: nil])
Can be used within a template to access a parameter. Parameters can be specified with `render_file`
directive. Optional value `default_value` allow to get a value if parameter has not been set.
It allows to create re-usable sub-templates that might be used in several places with several types
of parameters. Thus, templates can be called like kind of functions and generate output based on
the parameters provided.
Example:
```erb
render_file('show_service.html.erb', {service_name: 'service1', title: 'My Nice Service'})
[...]
render_file('show_service.html.erb', {service_name: 'service2', title: 'My Nicer Service'})
```
Note that you can also add parameters into a top level service using the command line:
```sh
consul-templaterb --template "source.html.erb:dest.html:reload_command:params.yaml"
[...]
```
See [samples/consul-ui/consul-services-ui.html.erb](samples/consul-ui/consul-services-ui.html.erb) for example of usage.
## secrets(prefix, [agent: vault_agent_address])
It requires that a Vault token is given either in parameter or in environment variable
The [policies](https://www.vaultproject.io/docs/concepts/policies.html) should be properly set.
List the secrets in vault under a given prefix.
Examples
### List all LDAP entities configured in Vault
```erb
<% ['users','groups'].each do |entity_type|
%><%= entity_type.capitalize %>: <%
secrets("auth/ldap/#{entity_type}/").each do |entity|
%> * <%=entity%>
<% end %>
<% end %>
```
Full example: [samples/vault-ldap.txt.erb](samples/vault-ldap.txt.erb)
## secret(path, [data = nil], [agent: vault_agent_address])
It requires that a Vault token is given either in parameter or in environment variable
The [policies](https://www.vaultproject.io/docs/concepts/policies.html) should be properly set.
Either read or write on a path in vault.
Having a non-nil data Hash will change the behavior from read to update and apply the given data.
Notice: For the moment the versionned KV abstration is not handled, if you want to access versioned KV, you have to hit the logical paths directly.
Examples
### Read LDAP configuration
```erb
secret('auth/ldap/config')['data']
```
Full example: [samples/vault-ldap.txt.erb](samples/vault-ldap.txt.erb)
### Read a path in non-versionned KV
```erb
secret('secret/foo', [force_ttl: intInSecond])
```
## remote_resource
### as_json(url, default_value, [refresh_delay_secs: intInSecond])
Fetch json data from any url. This allows to create templates with consul/vault data mixed in with data coming from other services/api.
Polling interval can be controlled with `refresh_delay_secs` option.
Request method (`GET`, `POST`, ...) can be controlled with `request_method` option.
```erb
remote_resource.as_json('http://my-api.dev/fridge/list.json', [])
```
Example with post (json_body will be applied `to_json` automatically):
```erb
remote_resource.as_json('http://my-api.dev/fridge', [], request_method: :post, json_body: {brand: 'any'})
```
Basic authentication can be done passing `headers` option.
```erb
remote_resource.as_json('http://my-api.dev/fridge/list.json', [], headers: { Authorization: [user, password]})
```
Full example: [samples/list_ruby_versions_from_rubygems.txt.erb](samples/list_ruby_versions_from_rubygems.txt.erb)
## template_info()
It returns information about current template being rendered.
The information returned has the following structure:
- `destination`: absolute path where the file is gonna be written
- `source`: the template absolute path file, most of the time, if will be
equal to `source_root`, except when the current template is included
in another template using `render_file()`
- `source_root`: the root template absolute file.
- `was_rendered_once` true if whole template structure has been rendered
at least once, false if current template data is still incomplete.
Examples
### Display template info
```erb
I am the file <%= File.basename(template_info['source']) %><%
if template_info['source'] != template_info['source_root']
%> included from template <%= File.basename(template_info['source_root']) %><%
end
%> rendered as <%= File.basename(template_info['destination']) %>
```
#### Simple rendering
The command `consul-template template_info.erb` would render:
```
I am the file template_info.erb rendered as template_info`
```
#### Rendering with template included into another
If creating a file called include.erb with contents: `<%= render_file('template_info.erb') %>`,
the command `consul-templaterb --template include.erb:destination_file.txt` would render:
```
I am the file template_info.erb included from template include.erb rendered as destination_file.txt`
```
## templates
It returns list of templates evaluated by this instance of consul-templaterb.
Information returned is an array of elements where elements are `[template_name, template_destination, args]`.
Example
### Display templates info
```erb
Here are templates rendered by consul-templaterb:
<% templates.each do |template, destination, args| %>
- I render <%= template %> with args <%= args.inspect %> and write the result to <%= destination %>
<% end %>
```