YYT的优势和作用
- 减少样板文件
- 最小化解析错误
- 通过覆盖修补
- 使配置可重用和可扩展
- 在一个工具中进行模板化和修补
- 安全、快速且确定性的执行
从简单实用的来说,yyt让YAML有了变量、函数、循环、条件等功能。
使用变量,例子#@ port = 15432
#@ port = 15432
source:
host: cdw
user: gpadmin
port: #@ port
运行 ytt -f template.yml > output.yml
source:
host: cdw
user: gpadmin
port: 15432
使用函数,例子#@ def dbname(id):
#@ port = 15432
#@ def dbname(id):
#@ return "testdb-" + id
#@ end
source:
host: cdw
user: gpadmin
port: #@ port
dbname: #@ dbname('100')
运行 ytt -f template.yaml > output.yaml
source:
host: cdw
user: gpadmin
port: 15432
dbname: testdb-100
使用for loop,例子#@ for id in data.values.ids:
#@ load("@ytt:data","data")
#@ def dbname(id):
#@ return "testdb-" + id
#@ end
#@ for id in data.values.ids:
---
source:
host: cdw
user: gpadmin
port: 15432
dbname: #@ dbname(id.name)
#@ end
values.yml的内容如下:
#@data/values
---
ids:
- name: '100'
- name: '101'
运行 ytt -f template.yml -f values.yml > output.yml
source:
host: cdw
user: gpadmin
port: 15432
dbname: testdb-100
---
source:
host: cdw
user: gpadmin
port: 15432
dbname: testdb-101
使用for condition,例子#@ for id in data.values.ids:
#@ load("@ytt:data","data")
#@ def dbname(id):
#@ return "testdb-" + id
#@ end
#@ for id in data.values.ids:
---
source:
host: cdw
user: gpadmin
port: 15432
dbname: #@ dbname(id.name)
#@ if id.drop:
drop: true
#@ end
#@ end
values.yml的内容如下:
#@data/values
---
ids:
- name: '101'
drop: true
- name: '102'
drop: false
运行 ytt -f template.yml -f values.yml > output.yml
source:
host: cdw
user: gpadmin
port: 15432
dbname: testdb-101
drop: true
---
source:
host: cdw
user: gpadmin
port: 15432
dbname: testdb-102
使用starlark定义变量
ytt目前还不能工作在yaml的anchor中Known Limitations
但是我们可以使用starlark等其它技术把这个绕过去。starlark是一个近似python的语法。
先在cpp.star文件里定义一个get_table()函数。
def get_table(dbname="testdb", schemaname="public", tablename="t1", skip="False"):
return {
"db": dbname,
"schema": schemaname,
"table": tablename,
"skip": "no" if skip == "False" else "yes",
}
end
以下是test.yml文件里的内容。
#@ load("cpp.star", "get_table")
source:
host: cdw
user: gpadmin
port: 15432
tables: #@ get_table()
运行ytt -f cpp.star -f test.yml > output.yml,结果如下。
source:
host: cdw
user: gpadmin
port: 15432
tables:
db: testdb
schema: public
table: t1
skip: "no"
最后修改时间:2024-06-26 11:48:09
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。