如何从模板组合的OS::Heat::ResourceGroup中正确获取资源属性?
问题:如何获取ResourceGroup创建的嵌套资源属性?
我想要创建模板中定义的部分资源的多个副本,请问如何获取以此方式创建的资源的属性?
为说明问题,我编写了一个用于生成随机字符串的模板(命名为word.yaml):
heat_template_version: rocky resources: word: type: OS::Heat::RandomString
由于需要生成此类随机字符串的列表,我使用OS::Heat::ResourceGroup调用word.yaml中的模板:
heat_template_version: rocky resources: composition: type: OS::Heat::ResourceGroup properties: count: 2 resource_def: type: word.yaml outputs: composition: value: {get_attr: [composition, resource.word]}
结合OS::Heat::ResourceGroup的说明文档和模板组合文档中关于嵌套属性的内容,我预期输出会是一个字符串列表,但实际得到如下错误:
{ "outputs": [ { "output_key": "composition", "description": "No description given", "output_error": "Member 'word' not found in group resource 'composition'.", "output_value": null } ] }
请问在ResourceGroup与模板组合的交互逻辑中,我忽略了什么要点?
解决方案
你忽略的核心要点是:OS::Heat::ResourceGroup无法直接访问嵌套模板内部的资源,只能获取嵌套模板主动输出的属性值。具体修正步骤如下:
- 首先需要在嵌套模板(
word.yaml)中定义输出,将内部资源的属性暴露出来 - 主模板中通过ResourceGroup获取这些输出值,而非直接引用嵌套模板内的资源名称
修改后的代码示例
- 更新
word.yaml,添加输出暴露随机字符串值:
heat_template_version: rocky resources: word: type: OS::Heat::RandomString outputs: random_str: value: {get_attr: [word, value]}
- 修改主模板,获取ResourceGroup中每个实例的输出值:
heat_template_version: rocky resources: composition: type: OS::Heat::ResourceGroup properties: count: 2 resource_def: type: word.yaml outputs: composition: value: {get_attr: [composition, random_str]}
这样就能得到包含两个随机字符串的列表,符合预期。
内容的提问来源于stack exchange,提问作者Frank
相关产品推荐
相关产品推荐

