编写一个 Hello World HOT 模板

HOT 是一种新的模板格式,旨在随着时间的推移取代与 CloudFormation 兼容的格式 (CFN),成为 Orchestration 模块支持的本机格式。本指南面向模板作者,并解释如何基于示例编写 HOT 模板。HOT 的详细规范可以在 Heat Orchestration Template (HOT) 规范 中找到。

本节介绍如何编写 HOT 模板,从非常基本的步骤开始,然后通过示例逐步深入。

最基本的模板

您可以想象到的最基本的模板仅包含一个资源定义,并且仅使用预定义的属性。例如,以下模板可用于部署单个计算实例

heat_template_version: 2015-04-30

description: Simple template to deploy a single compute instance

resources:
  my_instance:
    type: OS::Nova::Server
    properties:
      key_name: my_key
      image: ubuntu-trusty-x86_64
      flavor: m1.small

每个 HOT 模板都必须包含 heat_template_version 键,以及 HOT 版本值,例如 2013-05-23。请参阅 Heat 模板版本列表 以获取允许的值及其特性。

description 键是可选的,但是包含一些描述用户可以使用模板做什么的有用文本是一个好的做法。如果您想提供无法适应单行的更长的描述,则可以在 YAML 中提供多行文本,例如

description: >
  This is how you can provide a longer description
  of your template that goes over several lines.

resources 部分是必需的,并且必须包含至少一个资源定义。在上面的示例中,计算实例使用固定值定义了 key_nameimageflavor 属性。

注意

所有定义的元素(密钥对、镜像、flavor)都必须存在于使用模板的 OpenStack 环境中。

输入参数

模板的 parameters 部分中定义的输入参数允许用户在部署期间自定义模板。例如,这允许提供用于部署的自定义密钥对名称或镜像 ID。从模板作者的角度来看,这有助于通过避免硬编码假设来使模板更易于重用。

以下示例扩展了之前的模板,为资源的密钥对、镜像和 flavor 属性提供参数

heat_template_version: 2015-04-30

description: Simple template to deploy a single compute instance

parameters:
  key_name:
    type: string
    label: Key Name
    description: Name of key-pair to be used for compute instance
  image_id:
    type: string
    label: Image ID
    description: Image to be used for compute instance
  flavor:
    type: string
    label: Instance Type
    description: Type of instance (flavor) to be used

resources:
  my_instance:
    type: OS::Nova::Server
    properties:
      key_name: { get_param: key_name }
      image: { get_param: image_id }
      flavor: { get_param: flavor }

模板用户在部署堆栈期间必须为这三个参数定义值。get_param 内部函数检索用户为给定参数指定的值,并将其用于关联的资源属性。

有关内部函数的更多信息,请参阅 内部函数

提供默认值

您可以为参数提供默认值。如果用户未为参数定义值,则在堆栈部署期间将使用默认值。以下示例为 flavor 属性定义了默认值 m1.small

parameters:
  flavor:
    type: string
    label: Instance Type
    description: Flavor to be used
    default: m1.small

注意

如果模板未为参数定义默认值,则用户必须定义该值,否则堆栈创建将失败。

隐藏参数值

用户在部署堆栈时提供的数值可在堆栈详细信息中查看,并且可以被同一租户中的任何用户访问。要隐藏参数的值,请使用参数的 hidden 布尔属性

parameters:
  database_password:
    type: string
    label: Database Password
    description: Password to be used for database
    hidden: true

限制用户输入

您可以限制输入参数的值,以确保用户为该参数定义有效数据。constraints 属性定义了应用于参数的约束列表。以下示例将 flavor 参数限制为三个可能的值

parameters:
  flavor:
    type: string
    label: Instance Type
    description: Type of instance (flavor) to be used
    constraints:
      - allowed_values: [ m1.medium, m1.large, m1.xlarge ]
        description: Value must be one of m1.medium, m1.large or m1.xlarge.

以下示例为密码定义定义了多个约束

parameters:
  database_password:
    type: string
    label: Database Password
    description: Password to be used for database
    hidden: true
    constraints:
      - length: { min: 6, max: 8 }
        description: Password length must be between 6 and 8 characters.
      - allowed_pattern: "[a-zA-Z0-9]+"
        description: Password must consist of characters and numbers only.
      - allowed_pattern: "[A-Z]+[a-zA-Z0-9]*"
        description: Password must start with an uppercase character.

受支持的约束列表可在 参数约束 部分中找到。

注意

您可以定义相同类型的多个约束。特别是在允许模式的情况下,这不仅可以简化和维护正则表达式,还可以使向用户呈现的错误消息更加精确。

模板输出

除了通过输入参数自定义模板外,您还可以在模板的 outputs 部分向用户提供有关在堆栈部署期间创建的资源的信息。在以下示例中,输出部分提供了 my_instance 资源的 IP 地址

outputs:
  instance_ip:
    description: The IP address of the deployed instance
    value: { get_attr: [my_instance, first_address] }

注意

输出值通常使用内部函数(例如 get_attr)解析。有关内部函数的更多信息,请参阅 内部函数

有关 outputs 部分的更多信息,请参阅 输出部分