3.7. Jinja2 渲染
/usr/share/openstack-tripleo-heat-templates 中的核心 heat 模板包含具有 j2.yaml 文件扩展名的多个文件。这些文件包含 Jinja2 模板语法,director 会将这些文件呈现到具有 .yaml 扩展名的静态 heat 模板。例如,主要的 overcloud.j2.yaml 文件呈现到 overcloud.yaml 中。director 使用生成的 overcloud.yaml 文件。
通过 Jinja2 的 heat 模板,使用 Jinja2 语法为迭代值创建参数和资源。例如,overcloud.j2.yaml 文件包含以下代码片段:
parameters:
...
{% for role in roles %}
...
{{role.name}}Count:
description: Number of {{role.name}} nodes to deploy
type: number
default: {{role.CountDefault|default(0)}}
...
{% endfor %}
当 director 呈现 Jinja2 语法时,director 会迭代 roles_data.yaml 文件中定义的角色,并使用角色的名称填充 {{role.name}}Count 参数。默认 roles_data.yaml 文件包含五个角色,并从示例中生成以下参数:
-
ControllerCount -
ComputeCount -
BlockStorageCount -
ObjectStorageCount -
CephStorageCount
参数的渲染版本示例如下:
parameters:
...
ControllerCount:
description: Number of Controller nodes to deploy
type: number
default: 1
...
director 仅在核心 heat 模板的目录中呈现启用了 Jinja2 的模板和环境文件。以下用例演示了呈现 Jinja2 模板的正确方法。
使用案例 1:默认核心模板
模板目录: /usr/share/openstack-tripleo-heat-templates/
环境文件: /usr/share/openstack-tripleo-heat-templates/environments/ssl/enable-internal-tls.j2.yaml
director 使用默认核心模板位置(--templates),并将 enable-internal-tls.j2.yaml 文件呈现为 enable-internal-tls.yaml。运行 openstack overcloud deploy 命令时,请使用 -e 选项包含呈现的 enable-internal-tls.yaml 文件的名称。
$ openstack overcloud deploy --templates \
-e /usr/share/openstack-tripleo-heat-templates/environments/ssl/enable-internal-tls.yaml
...
使用案例 2:自定义核心模板
模板目录: /home/stack/tripleo-heat-installer-templates
环境文件: /home/stack/tripleo-heat-installer-templates/environments/ssl/enable-internal-tls.j2.yaml
director 使用自定义核心模板位置(--templates /home/stack/tripleo-heat-templates),director 会使自定义核心模板中的 enable-internal-tls.j2.yaml 文件呈现为 enable-internal-tls.yaml。运行 openstack overcloud deploy 命令时,请使用 -e 选项包含呈现的 enable-internal-tls.yaml 文件的名称。
$ openstack overcloud deploy --templates /home/stack/tripleo-heat-templates \
-e /home/stack/tripleo-heat-templates/environments/ssl/enable-internal-tls.yaml
...
使用案例 3:正确使用
模板目录: /usr/share/openstack-tripleo-heat-templates/
环境文件: /home/stack/tripleo-heat-installer-templates/environments/ssl/enable-internal-tls.j2.yaml
director 使用自定义核心模板位置(--templates /home/stack/tripleo-heat-installer-templates)。但是,所选的 enable-internal-tls.j2.yaml 不在自定义核心模板中,因此它不会呈现到 enable-internal-tls.yaml 中。这会导致部署失败。
在静态模板中处理 Jinja2 语法
使用 process-templates.py 脚本,将 openstack-tripleo-heat-templates 的 Jinja2 语法呈现为一组静态模板。要使用 process-templates.py 脚本呈现 openstack-tripleo-heat-templates 集合的副本,请更改到 openstack-tripleo-heat-templates 目录:
$ cd /usr/share/openstack-tripleo-heat-templates
运行 process-templates.py 脚本(位于 tools 目录中),并使用 -o 选项定义自定义目录来保存静态副本:
$ ./tools/process-templates.py -o ~/openstack-tripleo-heat-templates-rendered
这会将所有 Jinja2 模板转换为呈现的 YAML 版本,并将结果保存到 ~/openstack-tripleo-heat-templates-rendered。