confd template src格式和 templates 语法
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{}
- {{$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 is 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 is 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 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.
- {{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 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.
- {{ $url := split (getv "/deis/service") ":" }}
- host: {{index $url 0}}
- 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.
- {{range lookupSRV "mail" "tcp" "example.com"}}
- target: {{.Target}}
- port: {{.Port}}
- priority: {{.Priority}}
- weight: {{.Weight}}
- {{end}}
Add keys to etcd
- etcdctl set /services/zookeeper/host1 '{"Id":"host1", "IP":"192.168.10.11"}'
- etcdctl set /services/zookeeper/host2 '{"Id":"host2", "IP":"192.168.10.12"}'
Create the template resource
- [template]
- src = "services.conf.tmpl"
- dest = "/tmp/services.conf"
- keys = [
- "/services/zookeeper/"
- ]
Create the template
- {{range gets "/services/zookeeper/*"}}
- {{$data := json .Value}}
- id: {{$data.Id}}
- ip: {{$data.IP}}
- {{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:
- {
- "animals": [
- {"type": "dog", "name": "Fido"},
- {"type": "cat", "name": "Misse"}
- ]
- }
It can be traversed like this:
- {{$data := json (getv "/test/data/")}}
- type: {{ (index $data.animals 1).type }}
- name: {{ (index $data.animals 1).name }}
- {{range $data.animals}}
- {{.name}}
- {{end}}
jsonArray
Returns a []interface{} from a json array such as ["a", "b", "c"].
- {{range jsonArray (getv "/services/data/")}}
- val: {{.}}
- {{end}}
ls
Returns all subkeys, []string, where path matches its argument. Returns an empty list if path is not found.
- {{range ls "/deis/services"}}
- value: {{.}}
- {{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.
- {{range lsdir "/deis/services"}}
- value: {{.}}
- {{end}}
dir
Returns the parent directory of a given key.
- {{with dir "/services/data/url"}}
- dir: {{.}}
- {{end}}
join
Alias for the strings.Join function.
- {{$services := getvs "/services/elasticsearch/*"}}
- services: {{join $services ","}}
replace
Alias for the strings.Replace function.
- {{$backend := getv "/services/backend/nginx"}}
- 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.
- {{range lookupIP "some.host.local"}}
- server {{.}};
- {{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
- worker_processes {{getv "/nginx/worker_processes"}};
- upstream app {
- {{range getvs "/app/upstream/*"}}
- server {{.}};
- {{end}}
- }
- server {
- listen 80;
- server_name www.{{getv "/nginx/domain"}};
- access_log /var/log/nginx/{{getv "/nginx/domain"}}.access.log;
- error_log /var/log/nginx/{{getv "/nginx/domain"}}.log;
- location / {
- root {{getv "/nginx/root"}};
- index index.html index.htm;
- proxy_pass http://app;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- }
Output: /etc/nginx/nginx.conf
- worker_processes 2;
- upstream app {
- server 10.0.1.100:80;
- server 10.0.1.101:80;
- }
- server {
- listen 80;
- server_name www.example.com;
- access_log /var/log/nginx/example.com.access.log;
- error_log /var/log/nginx/example.com.error.log;
- location / {
- root /var/www/example_dotcom;
- index index.html index.htm;
- proxy_pass http://app;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- }
Complex example
This examples show how to use a combination of the templates functions to do nested iteration.
Add keys to etcd
- etcdctl mkdir /services/web/cust1/
- etcdctl mkdir /services/web/cust2/
- etcdctl set /services/web/cust1/2 '{"IP": "10.0.0.2"}'
- etcdctl set /services/web/cust2/2 '{"IP": "10.0.0.4"}'
- etcdctl set /services/web/cust2/1 '{"IP": "10.0.0.3"}'
- etcdctl set /services/web/cust1/1 '{"IP": "10.0.0.1"}'
Create the template resource
- [template]
- src = "services.conf.tmpl"
- dest = "/tmp/services.conf"
- keys = [
- "/services/web"
- ]
Create the template
- {{range $dir := lsdir "/services/web"}}
- upstream {{base $dir}} {
- {{$custdir := printf "/services/web/%s/*" $dir}}{{range gets $custdir}}
- server {{$data := json .Value}}{{$data.IP}}:80;
- {{end}}
- }
- server {
- server_name {{base $dir}}.example.com;
- location / {
- proxy_pass {{base $dir}};
- }
- }
- {{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 语法的更多相关文章
- Vue最常用的组件通讯有三种:父->子组件通讯、子->父组件通讯,兄弟组件通讯.(template用的pug模板语法)
Vue组件通讯 Vue最常用的组件通讯有三种:父->子组件通讯.子->父组件通讯,兄弟组件通讯.(template用的pug模板语法) 1.父->子组件通讯 父->子组件通 ...
- 【报错】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 ...
- 【报错】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 ...
- 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 ...
- shell编程--基本格式,基本语法,运算符,expr,(()),$[]
02/shell编程 Shell是用户与内核进行交互操作的一种接口,目前最流行的Shell称为bash Shell Shell也是一门编程语言."."号执行脚本时,会让脚本在调用者 ...
- MarkDownPad Pro 支持github格式的markdown语法
1. http://blog.csdn.net/xiaohei5188/article/details/43964451
- Notepad++ - 通过语言格式设置自定义语法高亮颜色
http://blog.csdn.net/onceing/article/details/51554399 Global Styles Indent guideline style 缩进参考线的颜色 ...
- ansible Templates
Files和templates files和templates均用于ansible文件处理,两者的主要区别是:Files目录下的文件无需写绝对路径即可将文件传输到远程主机,templates目录下文件 ...
- Ansible Playbooks 介绍 和 使用 二
目录 handlers playbook 案例 2 handlers vars 变量 setup facts 变量使用 案例 inventory 中定义变量 案例 条件测试 when 语句 案例 迭代 ...
随机推荐
- Mysql依赖库Boost的源码安装,linux下boost库的安装
boost‘准标准库’安装过程.安装的是boost_1_60_0. (1)首先去下载最新的boost代码包,网址www.boost.org. (2)进入到自己的目录,解压: bzip2 -d bo ...
- [转]关于ios 推送功能的终极解决
刚刚做了一个使用推送功能的应用 遇到了一些问题整的很郁闷 搞了两天总算是弄明白了 特此分享给大家 本帖 主要是针对产品发布版本的一些问题 综合了网上一些资料根据自己实践写的 不过测试也可以看看 首先要 ...
- [svc]linux上vxlan实战
linux vxlan实现2台机器的通往段ip互通 - 在n1上 ip l a vxlan0 type vxlan id 42 dstport 4789 remote 192.168.1.12 loc ...
- Fluent动网格【4】:DEFINE_CG_MOTION宏实例
DEFINE_CG_MOTION宏通常用于定义刚体部件的运动.本文以一个简单的案例描述DEFINE_CG_MOTION的使用方法. 案例描述 本次计算的案例如图所示.在计算域中有一个刚体块(图中的小正 ...
- pom.xml文件详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 15款css3鼠标悬停图片动画过渡特效
分享15款css3鼠标悬停图片动画过渡特效.这是一款15款不同效果的css3 hover动画过渡效果代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class ...
- 【Unity】EasyTouch5触屏检测
Unity AssetStore地址 https://assetstore.unity.com/packages/tools/input-management/easy-touch-5-touc ...
- Java知多少(51)finally
当异常被抛出,通常方法的执行将作一个陡峭的非线性的转向.依赖于方法是怎样编码的,异常甚至可以导致方法过早返回.这在一些方法中是一个问题.例如,如果一个方法打开一个文件项并关闭,然后退出,你不希望关闭文 ...
- Java JPA小记
什么是JPA JPA之于ORM(持久层框架,如MyBatis.Hibernate等)正如JDBC之于数据库驱动. JDBC是Java语言定义的一套标准,规范了客户端程序访问关系数据库(如MySQL.O ...
- [echarts] 横纵数据散点图
需求:课程平均分(X)与课程通过率散点图 http://echarts.baidu.com/echarts2/doc/example/scatter1.html https://www.cnblogs ...