如何在YAML文件中使用环境变量?
我正在使用kubectl创建一个命名空间,并想知道如何使用变量而不是testnamespace (如name: $var )。
apiVersion: v1
kind: Namespace
metadata:
name: testnamespace
spec:
finalizers:
- kubernetes发布于 2019-03-13 11:35:54
作为解决办法,您可以始终使用命令式的对象创建模式,而不是将变量合并到yaml文件中。
kubectl create namespace $NAME [--dry-run] [options]问题
示例
考虑下面的示例YAML。它是格式良好的YAML语法,但是它使用(非标准的)带内嵌表达式的大括号占位符。
嵌入的表达式不会在YAML中产生所需的结果,因为它们不是本机YAML规范的一部分。尽管如此,在本例中,它们只用于帮助说明标准YAML可用的是什么,哪些不是。
part01_customer_info:
cust_fname: "Homer"
cust_lname: "Himpson"
cust_motto: "I love donuts!"
cust_email: homer@himpson.org
part01_government_info:
govt_sales_taxrate: 1.15
part01_purchase_info:
prch_unit_label: "Bacon-Wrapped Fancy Glazed Donut"
prch_unit_price: 3.00
prch_unit_quant: 7
prch_product_cost: "{{prch_unit_price * prch_unit_quant}}"
prch_total_cost: "{{prch_product_cost * govt_sales_taxrate}}"
part02_shipping_info:
cust_fname: "{{cust_fname}}"
cust_lname: "{{cust_lname}}"
ship_city: Houston
ship_state: Hexas
part03_email_info:
cust_email: "{{cust_email}}"
mail_subject: Thanks for your DoughNutz order!
mail_notes: |
We want the mail_greeting to have all the expected values
with filled-in placeholders (and not curly-braces).
mail_greeting: |
Greetings {{cust_fname}} {{cust_lname}}!
We love your motto "{{cust_motto}}" and we agree with you!
Your total purchase price is {{prch_total_cost}}
Thank you for your order!解释
str.format)。

详细信息
YAML的一个经常被请求的特性是能够插入任意变量占位符,这些占位符支持与同一(或遮挡) YAML文件中的其他内容相关的任意交叉引用和表达式。
YAML支持锚点和别名,但此功能不支持任意放置YAML文本中任何位置的占位符和表达式。它们只适用于YAML节点。
YAML也支持自定义类型声明,但是它们并不常见,如果您接受来自可能不受信任的源的YAML内容,则存在安全问题。
YAML addon库
有YAML扩展库,但这些库不是本机YAML规范的一部分。
解决办法
sprintf或str.format样式功能另请参阅
https://stackoverflow.com/questions/55131000
复制相似问题