Heat模板内部函数
Heat模板内部函数又称为Intrinsic functions。
注:Intrinsic functions只能用在 resource 的 properties 段 和 outputs 中。
http://www.ysshang.com/openstackteam/p/5533111.html
1 get_attr
作用:获取所创建资源的属性。
语法:
get_attr: - <resource name> - <attribute name> - <key/index 1> (optional) - <key/index 2> (optional) - ...
Resource name:必须是模板 resouce 段中指定的资源。
Attribute name:要获取的属性,如果属性对应的值是list 或map, 则可以指定key/index来获取具体的值。
示例:
resources: my_instance: type: OS::Nova::Server # ... outputs: instance_ip: description: IP address of the deployed compute instance value: { get_attr: [my_instance, first_address] } instance_private_ip: description: Private IP address of the deployed compute instance value: { get_attr: [my_instance, networks, private, 0] }
2 get_file
作用:获取文件的内容。
语法:
get_file: <content key>
示例:
resources: my_instance: type: OS::Nova::Server properties: # general properties ... user_data: get_file: my_instance_user_data.sh my_other_instance: type: OS::Nova::Server properties: # general properties ... user_data: get_file: http://example.com/my_other_instance_user_data.sh
3 get_param
作用:引用模板中指定的参数。
语法:
get_param: - <parameter name> - <key/index 1> (optional) - <key/index 2> (optional) - ...
示例:
parameters: instance_type: type: string label: Instance Type description: Instance type to be used. server_data: type: json resources: my_instance: type: OS::Nova::Server properties: flavor: { get_param: instance_type} metadata: { get_param: [ server_data, metadata ] } key_name: { get_param: [ server_data, keys, 0 ] }
输入参数是:
{"instance_type": "m1.tiny", {"server_data": {"metadata": {"foo": "bar"}, "keys": ["a_key","other_key"]}}}
4 get_resource
作用:获取模板中指定的资源。
语法:
get_resource: <resource ID>
示例:
resources: instance_port: type: OS::Neutron::Port properties: ... instance: type: OS::Nova::Server properties: ... networks: port: { get_resource: instance_port }
5 list_join
作用:使用指定的分隔符将一个list中的字符串合成一个字符串。
语法:
list_join: - <delimiter> - <list to join>
示例输出:one, two, and three。
list_join: [', ', ['one', 'two', 'and three']]
6 digest
作用:在指定的值上使用algorithm。
语法:
digest: - <algorithm> - <value>
algorithm 可用的值是hashlib(md5, sha1, sha224, sha256, sha384, and sha512) 或openssl的相关值
示例:
# from a user supplied parameter pwd_hash: { digest: ['sha512', { get_param: raw_password }] }
7 repeat
作用:迭代fore_each中的列表,按照template的格式生成一个list。
语法:
repeat: template: <template> for_each: <var>: <list>
示例:
parameters: ports: type: comma_delimited_list label: ports default: "80,443,8080" protocols: type: comma_delimited_list label: protocols default: "tcp,udp" resources: security_group: type: OS::Neutron::SecurityGroup properties: name: web_server_security_group rules: repeat: for_each: <%port%>: { get_param: ports } <%protocol%>: { get_param: protocols } template: protocol: <%protocol%> port_range_min: <%port%>
结果是[{‘protocal’:tpc, ‘prot_range_min’:80},
{‘protocal’:tpc, ‘prot_range_min’:443},
{‘protocal’:tpc, ‘prot_range_min’:8080},
{‘protocal’:udp, ‘prot_range_min’:80},
{‘protocal’:udp, ‘prot_range_min’:443},
{‘protocal’:udp, ‘prot_range_min’:8080}]
8 resource_facade
作用:检索资源的数据。
语法:
resource_facade: <data type>
data type:metadata、deletion_policy、update_policy
9 str_replace
作用:使用params中的值替换template中的占位符,从而构造一个新的字符串。
语法:
str_replace: template: <template string> params: <parameter mappings>
示例:
resources: my_instance: type: OS::Nova::Server # general metadata and properties ... outputs: Login_URL: description: The URL to log into the deployed application value: str_replace: template: http://host/MyApplication params: host: { get_attr: [ my_instance, first_address ] }
template 中 host 将会被替换。
10 str_split
作用:将一个字符串按照分隔符分隔成一个list
语法:
str_split: - ',' - string,to,split
示例:
str_split: [',', 'string,to,split']
结果是['string', 'to', 'split']
11 map_merge
作用:合并多个map,且后面的map会覆盖前面map中同一个key的值。
语法:
map_merge: - <map 1> - <map 2> - ...
示例:
map_merge: [{'k1': 'v1', 'k2': 'v2'}, {'k1': 'v2'}]
结果是:{'k1': 'v2', 'k2': 'v2'}。
编者注:本文来自OpenStack开源团队工程师陈曾
http://docs.openstack.org/developer/heat/template_guide/hot_spec.html
2015-10-15 Heat 版本支持的内置函数
get_attr
get_file
get_param
get_resource
list_join
repeat
digest
resource_facade
str_replace
str_split
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端