CONFD template src format and templates syntax

Source: Internet
Author: User
Tags datetime split wrapper getv
Template Resources

Template Resources is written in TOML and define a single template resource. Template resources is stored under THE/ETC/CONFD/CONF.D directory by default. Required dest (String)-the target file. Keys (array of strings)-an array of keys. SRC (String)-the relative path of a configuration template. Optional GID (int)-The GID that should own the file. Defaults to the effective GID. Mode (String)-the permission mode of the file. UID (int)-The UID that should own the file. Defaults to the effective UID. Reload_cmd (String)-the command to reload Config. Check_cmd (String)-the command to check config. Use {{. src}} To reference the rendered source template. Prefix (String)-the string to prefix to keys. Notes

When using the Reload_cmd feature it's important that the command exits on its own. The reload command is not a managed by CONFD, and would block the configuration run until it exits. Example

[Template]
src = "Nginx.conf.tmpl"
dest = "/etc/nginx/nginx.conf"
uid = 0
gid = 0
mode = "0644"
keys = [
  "/ng Inx ",
]
check_cmd ="/usr/sbin/nginx-t-c {. src}} "
reload_cmd ="/usr/sbin/service nginx restart "




Templates

Templates define a single application configuration template. Templates is stored under The/etc/confd/templates directory by default.

Templates is written in Go s text/template. Template Functions Map

Creates a key-value map of string, interface{}

{{$endpoint: = map "name" "Elasticsearch" "Private_port" 9200 "Public_port" 443}}

Name: {{index $endpoint ' name '}}
Private-port: {{index $endpoint ' Private_port '}}
Public-port: {{index $ Endpoint "Public_port"}}

Specifically useful if you a sub-template and you want to pass multiple values to it. Base

Alias for the path. Base function.

{{with Get '/key '}}
    Key: {{base. Key}}
    value: {{. Value}}
{{end}}
exists

Checks if the key exists. Return False if key is not found.

{{if exists '/key '}}
    Value: {{Getv "/key"}}
{{end}}
Get

Returns the Kvpair where key matches its argument. Returns An error if key was not found.

{{with Get '/key '}}
    Key: {{. Key}}
    value: {{. Value}}
{{end}}
gets

Returns all Kvpair, []kvpair, where key matches its argument. Returns An error if key was not found.

{{range gets '/* '}}
    Key: {{. Key}}
    value: {{. Value}}
{{end}}
Getv

Returns the value as a string where key matches its argument or an optional default value. Returns An error if key was not found and no default value given.

Value: {{Getv '/key '}}
With a default value
Value: {{Getv "/key" "Default_value"}}
Getvs

Returns all values, []string, where key matches its argument. Returns An error if key was not found.

{{range Getvs '/* '}}
    Value: {{.}}
{{End}}
getenv

Wrapper for OS. Getenv. Retrieves the value of the environment variable named by the key. It returns the value, which would be the empty if the variable are not present. Optionally, you can give a default value that would be returned if the key was not present.

Export hostname= ' HOSTNAME '
Hostname: {{getenv ' hostname '}}
With a default value
IPAddr: {{getenv "host_ip" "127.0.0.1"}}
datetime

Alias for time. Now

# Generated by CONFD {{datetime}}

Outputs:

# Generated by CONFD 2015-01-23 13:34:56.093250283-0800 PST
# Generated by CONFD {{DateTime. Format "Jan 2, 2006 at 3:04pm (MST)"}}

Outputs:

# Generated by CONFD-Jan at 1:34pm (EST)

See the time usage:http://golang.org/pkg/time/ split

Wrapper for strings. Split. Splits the input string on the separating string and returns a slice of substrings.

{{$url: = Split (Getv "/deis/service") ":"}}
    Host: {{index $url 0}}
    port: {{index $url 1}}
ToUpper

Alias for strings. ToUpper Returns uppercased String.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.