如何在OS::Heat::ResourceGroup中以1开头%index%



我在OS::Heat::ResourceGroup中使用%index%,它以0值开始。我想让它从1开始。怎么做呢?

resources:
server_group:
type: OS::Heat::ResourceGroup
properties:
count: 3
resource_def:
type: OS::Nova::Server
name: { get_param: [ server_names, '%index%' ] }

我尝试了'%index% + 1',但它没有工作:

Error return: invalid literal for int() with base 10: '0 + 1'

您可以使用yaql和模板组合来实现这一点。以下是概念证明:

首先为您希望创建的资源创建一个模板,我将其命名为index.yaml:

heat_template_version: rocky
parameters:
input:
type: number
resources:
incrementor:
type: OS::Heat::Value
properties:
value:
yaql:
expression: 1 + int($.data.i)
data:
i: {get_param: input}
outputs:
index:
value: {get_attr: [incrementor, value]}

在您的具体情况中,您将把OS::Nova::Server放在这里。

从主模板(我叫它group.yaml)中使用这个模板,创建索引为递增的组:
heat_template_version: rocky
resources:
example:
type: OS::Heat::ResourceGroup
properties:
count: 10
resource_def:
type: index.yaml
properties:
input: "%index%"
outputs:
incremented_indicies:
value: {get_attr: [example, index]}

创建堆栈:

openstack stack create index -t group.yaml

您将看到值列表,从1开始,而不是从0开始:

openstack stack output show index --all   
+----------------------+-------------------------------------------+
| Field                | Value                                     |
+----------------------+-------------------------------------------+
| incremented_indicies | {                                         |
|                      |   "output_key": "incremented_indicies",   |
|                      |   "description": "No description given",  |
|                      |   "output_value": [                       |
|                      |     1,                                    |
|                      |     2,                                    |
|                      |     3,                                    |
|                      |     4,                                    |
|                      |     5,                                    |
|                      |     6,                                    |
|                      |     7,                                    |
|                      |     8,                                    |
|                      |     9,                                    |
|                      |     10                                    |
|                      |   ]                                       |
|                      | }                                         |
+----------------------+-------------------------------------------+

你问为什么要模板组合?最初,我希望它像这样工作:

heat_template_version: rocky
resources:
example:
type: OS::Heat::ResourceGroup
properties:
count: 10
resource_def:
type: OS::Heat::Value
properties:
value:
yaql:
expression: 1 + int($.data.i)
data:
i: "%index%"
outputs:
sizes:
value: {get_attr: [example, value]}

但是,它不会,因为在填充索引值之前,yaql似乎已经被求值了:

ERROR: Property error: : resources.example.properties.resource_def: : invalid literal for int() with base 10: '%index%'

所以模板组合是一个解决方法。

最新更新