Template Resources

Template resources are written in TOML and define a single template resource. Template resources are 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 managed by confd, and will 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 = [
"/nginx",
]
check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
reload_cmd = "/usr/sbin/service nginx restart"
 
 
 

Templates

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

Templates are written in Go's text/template.

Template Functions

map

creates a key-value map of string -> interface{}

  1.  
    {{$endpoint := map "name" "elasticsearch" "private_port" 9200 "public_port" 443}}
  2.  
     
  3.  
    name: {{index $endpoint "name"}}
  4.  
    private-port: {{index $endpoint "private_port"}}
  5.  
    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.

  1.  
    {{with get "/key"}}
  2.  
    key: {{base .Key}}
  3.  
    value: {{.Value}}
  4.  
    {{end}}

exists

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

  1.  
    {{if exists "/key"}}
  2.  
    value: {{getv "/key"}}
  3.  
    {{end}}

get

Returns the KVPair where key matches its argument. Returns an error if key is not found.

  1.  
    {{with get "/key"}}
  2.  
    key: {{.Key}}
  3.  
    value: {{.Value}}
  4.  
    {{end}}

gets

Returns all KVPair, []KVPair, where key matches its argument. Returns an error if key is not found.

  1.  
    {{range gets "/*"}}
  2.  
    key: {{.Key}}
  3.  
    value: {{.Value}}
  4.  
    {{end}}

getv

Returns the value as a string where key matches its argument or an optional default value. Returns an error if key is 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 is not found.

  1.  
    {{range getvs "/*"}}
  2.  
    value: {{.}}
  3.  
    {{end}}

getenv

Wrapper for os.Getenv. Retrieves the value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present. Optionally, you can give a default value that will be returned if the key is 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 23, 2015 at 1:34pm (EST)

See the time package for more 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.

  1.  
    {{ $url := split (getv "/deis/service") ":" }}
  2.  
    host: {{index $url 0}}
  3.  
    port: {{index $url 1}}

toUpper

Alias for strings.ToUpper Returns uppercased string.

key: {{toUpper "value"}}

toLower

Alias for strings.ToLower. Returns lowercased string.

key: {{toLower "Value"}}

json

Returns an map[string]interface{} of the json value.

lookupSRV

Wrapper for net.LookupSRV. The wrapper also sorts the SRV records alphabetically by combining all the fields of the net.SRV struct to reduce unnecessary config reloads.

  1.  
    {{range lookupSRV "mail" "tcp" "example.com"}}
  2.  
    target: {{.Target}}
  3.  
    port: {{.Port}}
  4.  
    priority: {{.Priority}}
  5.  
    weight: {{.Weight}}
  6.  
    {{end}}

Add keys to etcd

  1.  
    etcdctl set /services/zookeeper/host1 '{"Id":"host1", "IP":"192.168.10.11"}'
  2.  
    etcdctl set /services/zookeeper/host2 '{"Id":"host2", "IP":"192.168.10.12"}'

Create the template resource

  1.  
    [template]
  2.  
    src = "services.conf.tmpl"
  3.  
    dest = "/tmp/services.conf"
  4.  
    keys = [
  5.  
    "/services/zookeeper/"
  6.  
    ]

Create the template

  1.  
    {{range gets "/services/zookeeper/*"}}
  2.  
    {{$data := json .Value}}
  3.  
    id: {{$data.Id}}
  4.  
    ip: {{$data.IP}}
  5.  
    {{end}}

Advanced Map Traversals

Once you have parsed the JSON, it is possible to traverse it with normal Go template functions such as index.

A more advanced structure, like this:

  1.  
    {
  2.  
    "animals": [
  3.  
    {"type": "dog", "name": "Fido"},
  4.  
    {"type": "cat", "name": "Misse"}
  5.  
    ]
  6.  
    }

It can be traversed like this:

  1.  
    {{$data := json (getv "/test/data/")}}
  2.  
    type: {{ (index $data.animals 1).type }}
  3.  
    name: {{ (index $data.animals 1).name }}
  4.  
    {{range $data.animals}}
  5.  
    {{.name}}
  6.  
    {{end}}

jsonArray

Returns a []interface{} from a json array such as ["a", "b", "c"].

  1.  
    {{range jsonArray (getv "/services/data/")}}
  2.  
    val: {{.}}
  3.  
    {{end}}

ls

Returns all subkeys, []string, where path matches its argument. Returns an empty list if path is not found.

  1.  
    {{range ls "/deis/services"}}
  2.  
    value: {{.}}
  3.  
    {{end}}

lsdir

Returns all subkeys, []string, where path matches its argument. It only returns subkeys that also have subkeys. Returns an empty list if path is not found.

  1.  
    {{range lsdir "/deis/services"}}
  2.  
    value: {{.}}
  3.  
    {{end}}

dir

Returns the parent directory of a given key.

  1.  
    {{with dir "/services/data/url"}}
  2.  
    dir: {{.}}
  3.  
    {{end}}

join

Alias for the strings.Join function.

  1.  
    {{$services := getvs "/services/elasticsearch/*"}}
  2.  
    services: {{join $services ","}}

replace

Alias for the strings.Replace function.

  1.  
    {{$backend := getv "/services/backend/nginx"}}
  2.  
    backend = {{replace $backend "-" "_" -1}}

lookupIP

Wrapper for net.LookupIP function. The wrapper also sorts (alphabeticaly) the IP addresses. This is crucial since in dynamic environments DNS servers typically shuffle the addresses linked to domain name. And that would cause unnecessary config reloads.

  1.  
    {{range lookupIP "some.host.local"}}
  2.  
    server {{.}};
  3.  
    {{end}}

Example Usage

etcdctl set /nginx/domain 'example.com'
etcdctl set /nginx/root '/var/www/example_dotcom'
etcdctl set /nginx/worker_processes '2'
etcdctl set /app/upstream/app1 "10.0.1.100:80"
etcdctl set /app/upstream/app2 "10.0.1.101:80"

/etc/confd/templates/nginx.conf.tmpl

  1.  
    worker_processes {{getv "/nginx/worker_processes"}};
  2.  
     
  3.  
    upstream app {
  4.  
    {{range getvs "/app/upstream/*"}}
  5.  
    server {{.}};
  6.  
    {{end}}
  7.  
    }
  8.  
     
  9.  
    server {
  10.  
    listen 80;
  11.  
    server_name www.{{getv "/nginx/domain"}};
  12.  
    access_log /var/log/nginx/{{getv "/nginx/domain"}}.access.log;
  13.  
    error_log /var/log/nginx/{{getv "/nginx/domain"}}.log;
  14.  
     
  15.  
    location / {
  16.  
    root {{getv "/nginx/root"}};
  17.  
    index index.html index.htm;
  18.  
    proxy_pass http://app;
  19.  
    proxy_redirect off;
  20.  
    proxy_set_header Host $host;
  21.  
    proxy_set_header X-Real-IP $remote_addr;
  22.  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  23.  
    }
  24.  
    }

Output: /etc/nginx/nginx.conf

  1.  
    worker_processes 2;
  2.  
     
  3.  
    upstream app {
  4.  
    server 10.0.1.100:80;
  5.  
    server 10.0.1.101:80;
  6.  
    }
  7.  
     
  8.  
    server {
  9.  
    listen 80;
  10.  
    server_name www.example.com;
  11.  
    access_log /var/log/nginx/example.com.access.log;
  12.  
    error_log /var/log/nginx/example.com.error.log;
  13.  
     
  14.  
    location / {
  15.  
    root /var/www/example_dotcom;
  16.  
    index index.html index.htm;
  17.  
    proxy_pass http://app;
  18.  
    proxy_redirect off;
  19.  
    proxy_set_header Host $host;
  20.  
    proxy_set_header X-Real-IP $remote_addr;
  21.  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  22.  
    }
  23.  
    }

Complex example

This examples show how to use a combination of the templates functions to do nested iteration.

Add keys to etcd

  1.  
    etcdctl mkdir /services/web/cust1/
  2.  
    etcdctl mkdir /services/web/cust2/
  3.  
    etcdctl set /services/web/cust1/2 '{"IP": "10.0.0.2"}'
  4.  
    etcdctl set /services/web/cust2/2 '{"IP": "10.0.0.4"}'
  5.  
    etcdctl set /services/web/cust2/1 '{"IP": "10.0.0.3"}'
  6.  
    etcdctl set /services/web/cust1/1 '{"IP": "10.0.0.1"}'

Create the template resource

  1.  
    [template]
  2.  
    src = "services.conf.tmpl"
  3.  
    dest = "/tmp/services.conf"
  4.  
    keys = [
  5.  
    "/services/web"
  6.  
    ]

Create the template

  1.  
    {{range $dir := lsdir "/services/web"}}
  2.  
    upstream {{base $dir}} {
  3.  
    {{$custdir := printf "/services/web/%s/*" $dir}}{{range gets $custdir}}
  4.  
    server {{$data := json .Value}}{{$data.IP}}:80;
  5.  
    {{end}}
  6.  
    }
  7.  
     
  8.  
    server {
  9.  
    server_name {{base $dir}}.example.com;
  10.  
    location / {
  11.  
    proxy_pass {{base $dir}};
  12.  
    }
  13.  
    }
  14.  
    {{end}}

Output:/tmp/services.conf



原文   https://blog.csdn.net/ztsinghua/article/details/51643732
官网 https://github.com/kelseyhightower/confd/blob/master/docs/templates.md

confd template src格式和 templates 语法的更多相关文章

  1. Vue最常用的组件通讯有三种:父->子组件通讯、子->父组件通讯,兄弟组件通讯.(template用的pug模板语法)

    Vue组件通讯   Vue最常用的组件通讯有三种:父->子组件通讯.子->父组件通讯,兄弟组件通讯.(template用的pug模板语法) 1.父->子组件通讯 父->子组件通 ...

  2. 【报错】An error happened during template parsing (template: "class path resource [templates/adminManageCourse.html]")

    页面显示: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing t ...

  3. 【报错】An error happened during template parsing (template: "class path resource [templates/hello1.html]")

    页面显示: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing t ...

  4. Thymeleaf 异常:Exception processing template "index": An error happened during template parsing (template: "class path resource [templates/index.html]")

    Spring Boot 项目,在 Spring Tool Suite 4, Version: 4.4.0.RELEASE 运行没有问题,将项目中的静态资源和页面复制到 IDEA 的项目中,除了 IDE ...

  5. shell编程--基本格式,基本语法,运算符,expr,(()),$[]

    02/shell编程 Shell是用户与内核进行交互操作的一种接口,目前最流行的Shell称为bash Shell Shell也是一门编程语言."."号执行脚本时,会让脚本在调用者 ...

  6. MarkDownPad Pro 支持github格式的markdown语法

    1. http://blog.csdn.net/xiaohei5188/article/details/43964451

  7. Notepad++ - 通过语言格式设置自定义语法高亮颜色

    http://blog.csdn.net/onceing/article/details/51554399 Global Styles Indent guideline style  缩进参考线的颜色 ...

  8. ansible Templates

    Files和templates files和templates均用于ansible文件处理,两者的主要区别是:Files目录下的文件无需写绝对路径即可将文件传输到远程主机,templates目录下文件 ...

  9. Ansible Playbooks 介绍 和 使用 二

    目录 handlers playbook 案例 2 handlers vars 变量 setup facts 变量使用 案例 inventory 中定义变量 案例 条件测试 when 语句 案例 迭代 ...

随机推荐

  1. SNF开发平台WinForm-平板拍照及扫描二维码功能

    在我们做项目的时候,经常会有移动平板处理检验,审核等,方便移动办公.这时就需要在现场拍照上传问题,把当场问题进行上传,也有已经拍完照的图片或加工过的图片进行上传.还有在车间现场一体机,工控机 这种产物 ...

  2. Lua Linux环境下安装

    首先下载Lua包 解压缩 进入执行make linux 如果碰到lua.c:67:31: fatal error: readline/readline.h: No such file or direc ...

  3. ceph 的 bufferlist

    bufferlist是buffer::list的别名,其由来在 http://bean-li.github.io/bufferlist-in-ceph/ 中有非常详细的介绍 其p.p_off.off字 ...

  4. 《转》推荐几个精致的web UI框架

    1.Aliceui Aliceui是支付宝的样式解决方案,是一套精选的基于 spm 生态圈的样式模块集合,是 Arale 的子集,也是一套模块化的样式命名和组织规范,是写 CSS 的更好方式. git ...

  5. linux每日命令(9):cp命令

    一.命令格式: cp [参数] source dest 或 cp [参数] source... directory 二.命令功能: 将源文件复制至目标文件,或将多个源文件复制至目标目录. 三. 命令参 ...

  6. 测试覆盖率工具:EclEmma

    测试覆盖率工具:EclEmma 2016-08-26 目录 1 测试覆盖率实现技术2 EclEmma介绍3 EclEmma测试覆盖率指标4 EclEmma安装5 示例项目介绍  5.1 创建项目  5 ...

  7. 【Spring源码分析】Bean加载流程概览(转)

    转载自:https://www.cnblogs.com/xrq730/p/6285358.html 代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. ...

  8. yum只下载软件不安装的两种方法

    1 通过yum自带一个工具:yumdownloader rpm -qa |grep yum-utils yum -y install yum-utils* rpm -ql yum-utils 安装好后 ...

  9. 法线从object space到eye space的转换((normal matrix)

    对于顶点来说,从object Space转换到eye space, 使用model-view矩阵就好了.那么顶点的法线是否也可以直接使用model-view矩阵转化? 通常情况下是不行的. 如下两张图 ...

  10. MSP MCU I2C入门指南

    这是一份介绍性指南,指导你如何用超低功耗MSP微控制器 (MCU) 开始一个与I2C通信有关的项目: 简介 I2C(或称为I2C,集成电路总线)是一种两线制通信形式,主要用来在短距离.电路板间的应用中 ...