Chapter 2. Understanding heat templates
Chapter 2. Understanding heat templates The custom configurations in this guide use heat templates and environment files to define certain aspects of the overcloud. This chapter provides a basic introduction to heat templates so that you can understand the structure and format of these templates in the context of Red Hat OpenStack Platform director. 2.1. heat templates Director uses Heat Orchestration Templates (HOT) as the template format for the overcloud deployment plan. Templates in HOT format are usually expressed in YAML format. The purpose of a template is to define and create a stack, which is a collection of resources that OpenStack Orchestration (heat) creates, and the configuration of the resources. Resources are objects in Red Hat OpenStack Platform (RHOSP) and can include compute resources, network configuration, security groups, scaling rules, and custom resources. A heat template has three main sections: parameters These are settings passed to heat, which provide a way to customize a stack, and any default values for parameters without passed values. These settings are defined in the parameters section of a template. resources Use the resources section to define the resources, such as compute instances, networks, and storage volumes, that you can create when you deploy a stack using this template. Red Hat OpenStack Platform (RHOSP) contains a set of core resources that span across all components. These are the specific objects to create and configure as part of a stack. outputs Use the outputs section to declare the output parameters that your cloud users can access after the stack is created. Your cloud users can use these parameters to request details about the stack, such as the IP addresses of deployed instances, or URLs of web applications deployed as part of the stack. Example of a basic heat template: This template uses the resource type type: OS::Nova::Server to create an instance called my_instance with a particular flavor, image, and key that the cloud user specifies. The stack can return the value of instance_name , which is called My Cirros Instance . When heat processes a template, it creates a stack for the template and a set of child stacks for resource templates. This creates a hierarchy of stacks that descend from the main stack that you define with your template. You can view the stack hierarchy with the following command: 2.2. Environment files An environment file is a special type of template that you can use to customize your heat templates. You can include environment files in the deployment command, in addition to the core heat templates. An environment file contains three main sections: resource_registry This section defines custom resource names, linked to other heat templates. This provides a method to create custom resources that do not exist within the core resource collection. parameters These are common settings that you apply to the parameters of the top-level template. For example, if you have a template that deploys nested stacks, such as resource registry mappings, the parameters apply only to the top-level template and not to templates for the nested resources. parameter_defaults These parameters modify the default values for parameters in all templates. For example, if you have a heat template that deploys nested stacks, such as resource registry mappings,the parameter defaults apply to all templates. Important Use parameter_defaults instead of parameters when you create custom environment files for your overcloud, so that your parameters apply to all stack templates for the overcloud. Example of a basic environment file: This environment file ( my_env.yaml ) might be included when creating a stack from a certain heat template ( my_template.yaml ). The my_env.yaml file creates a new resource type called OS::Nova::Server::MyServer . The myserver.yaml file is a heat template file that provides an implementation for this resource type that overrides any built-in ones. You can include the OS::Nova::Server::MyServer resource in your my_template.yaml file. MyIP applies a parameter only to the main heat template that deploys with this environment file. In this example, MyIP applies only to the parameters in my_template.yaml . NetworkName applies to both the main heat template, my_template.yaml , and the templates that are associated with the resources that are included in the main template, such as the OS::Nova::Server::MyServer resource and its myserver.yaml template in this example. Note For RHOSP to use the heat template file as a custom template resource, the file extension must be either .yaml or .template. 2.3. Core overcloud heat templates Director contains a core heat template collection and environment file collection for the overcloud. This collection is stored in /usr/share/openstack-tripleo-heat-templates . The main files and directories in this template collection are: overcloud.j2.yaml This is the main template file that director uses to create the overcloud environment. This file uses Jinja2 syntax to iterate over certain sections in the template to create custom roles. The Jinja2 formatting is rendered into YAML during the overcloud deployment process. overcloud-resource-registry-puppet.j2.yaml This is the main environment file that director uses to create the overcloud environment. It provides a set of configurations for Puppet modules stored on the overcloud image. After director writes the overcloud image to each node, heat starts the Puppet configuration for each node by using the resources registered in this environment file. This file uses Jinja2 syntax to iterate over certain sections in the template to create custom roles. The Jinja2 formatting is rendered into YAML during the overcloud deployment process. roles_data.yaml This file contains the definitions of the roles in an overcloud and maps services to each role. network_data.yaml This file contains the definitions of the networks in an overcloud and their properties such as subnets, allocation pools, and VIP status. The default network_data.yaml file contains the default networks: External, Internal Api, Storage, Storage Management, Tenant, and Management. You can create a custom network_data.yaml file and add it to your openstack overcloud deploy command with the -n option. plan-environment.yaml This file contains the definitions of the metadata for your overcloud plan. This includes the plan name, main template to use, and environment files to apply to the overcloud. capabilities-map.yaml This file contains a mapping of environment files for an overcloud plan. deployment This directory contains heat templates. The overcloud-resource-registry-puppet.j2.yaml environment file uses the files in this directory to drive the application of the Puppet configuration on each node. environments This directory contains additional heat environment files that you can use for your overcloud creation. These environment files enable extra functions for your resulting Red Hat OpenStack Platform (RHOSP) environment. For example, the directory contains an environment file to enable Cinder NetApp backend storage ( cinder-netapp-config.yaml ). network This directory contains a set of heat templates that you can use to create isolated networks and ports. puppet This directory contains templates that control Puppet configuration. The overcloud-resource-registry-puppet.j2.yaml environment file uses the files in this directory to drive the application of the Puppet configuration on each node. puppet/services This directory contains legacy heat templates for all service configuration. The templates in the deployment directory replace most of the templates in the puppet/services directory. extraconfig This directory contains templates that you can use to enable extra functionality. firstboot This directory contains example first_boot scripts that director uses when initially creating the nodes. 2.4. Plan environment metadata You can define metadata for your overcloud plan in a plan environment metadata file. Director applies metadata during the overcloud creation, and when importing and exporting your overcloud plan. Use plan environment files to define workflows which director can execute with the OpenStack Workflow (Mistral) service. A plan environment metadata file includes the following parameters: version The version of the template. name The name of the overcloud plan and the container in OpenStack Object Storage (swift) that you want to use to store the plan files. template The core parent template that you want to use for the overcloud deployment. This is most often overcloud.yaml , which is the rendered version of the overcloud.yaml.j2 template. environments Defines a list of environment files that you want to use. Specify the name and relative locations of each environment file with the path sub-parameter. parameter_defaults A set of parameters that you want to use in your overcloud. This functions in the same way as the parameter_defaults section in a standard environment file. passwords A set of parameters that you want to use for overcloud passwords. This functions in the same way as the parameter_defaults section in a standard environment file. Usually, the director populates this section automatically with randomly generated passwords. workflow_parameters Use this parameter to provide a set of parameters to OpenStack Workflow (mistral) namespaces. You can use this to calculate and automatically generate certain overcloud parameters. The following snippet is an example of the syntax of a plan environment file: You can include the plan environment metadata file with the openstack overcloud deploy command with the -p option: You can also view plan metadata for an existing overcloud plan with the following command: 2.5. Including environment files in overcloud creation Include environment files in the deployment command with the -e option. You can include as many environment files as necessary. However, the order of the environment files is important as the parameters and resources that you define in subsequent environment files take precedence. For example, you have two environment files that contain a common resource type OS::TripleO::NodeExtraConfigPost , and a common parameter TimeZone : environment-file-1.yaml environment-file-2.yaml You include both environment files in the deployment command: The openstack overcloud deploy command runs through the following process: Loads the default configuration from the core heat template collection. Applies the configuration from environment-file-1.yaml , which overrides any common settings from the default configuration. Applies the configuration from environment-file-2.yaml , which overrides any common settings from the default configuration and environment-file-1.yaml . This results in the following changes to the default configuration of the overcloud: OS::TripleO::NodeExtraConfigPost resource is set to /home/stack/templates/template-2.yaml , as defined in environment-file-2.yaml . TimeZone parameter is set to Hongkong , as defined in environment-file-2.yaml . RabbitFDLimit parameter is set to 65536 , as defined in environment-file-1.yaml . environment-file-2.yaml does not change this value. You can use this mechanism to define custom configuration for your overcloud without values from multiple environment files conflicting. 2.6. Using customized core heat templates When creating the overcloud, director uses a core set of heat templates located in /usr/share/openstack-tripleo-heat-templates . If you want to customize this core template collection, use the following Git workflows to manage your custom template collection: Procedure Create an initial Git repository that contains the heat template collection: Copy the template collection to the /home/stack/templates directory: Change to the custom template directory and initialize a Git repository: Configure your Git user name and email address: Replace <USER_NAME> with the user name that you want to use. Replace <EMAIL_ADDRESS> with your email address. Stage all templates for the initial commit: Create an initial commit: This creates an initial master branch that contains the latest core template collection. Use this branch as the basis for your custom branch and merge new template versions to this branch. Use a custom branch to store your changes to the core template collection. Use the following procedure to create a my-customizations branch and add customizations: Create the my-customizations branch and switch to it: Edit the files in the custom branch. Stage the changes in git: Commit the changes to the custom branch: This adds your changes as commits to the my-customizations branch. When the master branch updates, you can rebase my-customizations off master , which causes git to add these commits on to the updated template collection. This helps track your customizations and replay them on future template updates. When you update the undercloud, the openstack-tripleo-heat-templates package might also receive updates. When this occurs, you must also update your custom template collection: Save the openstack-tripleo-heat-templates package version as an environment variable: Change to your template collection directory and create a new branch for the updated templates: Remove all files in the branch and replace them with the new versions: Add all templates for the initial commit: Create a commit for the package update: Merge the branch into master. If you use a Git management system (such as GitLab), use the management workflow. If you use git locally, merge by switching to the master branch and run the git merge command: The master branch now contains the latest version of the core template collection. You can now rebase the my-customization branch from this updated collection. Update the my-customization branch,: Change to the my-customizations branch: Rebase the branch off master : This updates the my-customizations branch and replays the custom commits made to this branch. Resolve any conflicts that occur during the rebase: Check which files contain the conflicts: Resolve the conflicts of the template files identified. Add the resolved files: Continue the rebase: Deploy the custom template collection: Ensure that you have switched to the my-customization branch: Run the openstack overcloud deploy command with the --templates option to specify your local template directory: Note Director uses the default template directory ( /usr/share/openstack-tripleo-heat-templates ) if you specify the --templates option without a directory. Important Red Hat recommends using the methods in Chapter 4, Configuration hooks instead of modifying the heat template collection. 2.7. Jinja2 rendering The core heat templates in /usr/share/openstack-tripleo-heat-templates contain a number of files that have the j2.yaml file extension. These files contain Jinja2 template syntax and director renders these files to their static heat template equivalents that have the .yaml extension. For example, the main overcloud.j2.yaml file renders into overcloud.yaml . Director uses the resulting overcloud.yaml file. The Jinja2-enabled heat templates use Jinja2 syntax to create parameters and resources for iterative values. For example, the overcloud.j2.yaml file contains the following snippet: When director renders the Jinja2 syntax, director iterates over the roles defined in the roles_data.yaml file and populates the {{role.name}}Count parameter with the name of the role. The default roles_data.yaml file contains five roles and results in the following parameters from our example: ControllerCount ComputeCount BlockStorageCount ObjectStorageCount CephStorageCount A example rendered version of the parameter looks like this: Director renders Jinja2-enabled templates and environment files only from within the directory of your core heat templates. The following use cases demonstrate the correct method to render the Jinja2 templates. Use case 1: Default core templates Template directory: /usr/share/openstack-tripleo-heat-templates/ Environment file: /usr/share/openstack-tripleo-heat-templates/environments/network-isolation.j2.yaml Director uses the default core template location ( --templates ) and renders the network-isolation.j2.yaml file into network-isolation.yaml . When you run the openstack overcloud deploy command, use the -e option to include the name of the rendered network-isolation.yaml file. Use case 2: Custom core templates Template directory: /home/stack/tripleo-heat-templates Environment file: /home/stack/tripleo-heat-templates/environments/network-isolation.j2.yaml Director uses a custom core template location ( --templates /home/stack/tripleo-heat-templates ) and director renders the network-isolation.j2.yaml file within the custom core templates into network-isolation.yaml . When you run the openstack overcloud deploy command, use the -e option to include the name of the rendered network-isolation.yaml file. Use case 3: Incorrect usage Template directory: /usr/share/openstack-tripleo-heat-templates/ Environment file: /home/stack/tripleo-heat-templates/environments/network-isolation.j2.yaml Director uses a default core template location ( --templates /usr/share/openstack-tripleo-heat-templates ). However, the chosen network-isolation.j2.yaml is not located within the custom core templates, so it will not render into network-isolation.yaml . This causes the deployment to fail. Processing Jinja2 syntax into static templates Use the process-templates.py script to render the Jinja2 syntax of the openstack-tripleo-heat-templates into a set of static templates. To render a copy of the openstack-tripleo-heat-templates collection with the process-templates.py script, change to the openstack-tripleo-heat-templates directory: Run the process-templates.py script, which is located in the tools directory, along with the -o option to define a custom directory to save the static copy: This converts all Jinja2 templates to their rendered YAML versions and saves the results to ~/openstack-tripleo-heat-templates-rendered .
heat_template_version: 2013-05-23 description: > A very basic Heat template. parameters: key_name: type: string default: lars description: Name of an existing key pair to use for the instance flavor: type: string description: Instance type for the instance to be created default: m1.small image: type: string default: cirros description: ID or name of the image to use for the instance resources: my_instance: type: OS::Nova::Server properties: name: My Cirros Instance image: { get_param: image } flavor: { get_param: flavor } key_name: { get_param: key_name } output: instance_name: description: Get the instance's name value: { get_attr: [ my_instance, name ] } openstack stack list --nested resource_registry: OS::Nova::Server::MyServer: myserver.yaml parameter_defaults: NetworkName: my_network parameters: MyIP: 192.168.0.1 version: 1.0 name: myovercloud description: 'My Overcloud Plan' template: overcloud.yaml environments: - path: overcloud-resource-registry-puppet.yaml - path: environments/containers-default-parameters.yaml - path: user-environment.yaml parameter_defaults: ControllerCount: 1 ComputeCount: 1 OvercloudComputeFlavor: compute OvercloudControllerFlavor: control workflow_parameters: tripleo.derive_params.v1.derive_parameters: num_phy_cores_per_numa_node_for_pmd: 2 (undercloud) USD openstack overcloud deploy --templates -p /my-plan-environment.yaml [OTHER OPTIONS] (undercloud) USD openstack object save overcloud plan-environment.yaml --file - resource_registry: OS::TripleO::NodeExtraConfigPost: /home/stack/templates/template-1.yaml parameter_defaults: RabbitFDLimit: 65536 TimeZone: 'Japan' resource_registry: OS::TripleO::NodeExtraConfigPost: /home/stack/templates/template-2.yaml parameter_defaults: TimeZone: 'Hongkong' openstack overcloud deploy --templates -e environment-file-1.yaml -e environment-file-2.yaml cd ~/templates cp -r /usr/share/openstack-tripleo-heat-templates . cd ~/templates/openstack-tripleo-heat-templates git init . git config --global user.name "<USER_NAME>" git config --global user.email "<EMAIL_ADDRESS>" git add * git commit -m "Initial creation of custom core heat templates" git checkout -b my-customizations git add [edited files] git commit -m " [Commit message for custom changes] " export PACKAGE=USD(rpm -qv openstack-tripleo-heat-templates) cd ~/templates/openstack-tripleo-heat-templates git checkout -b USDPACKAGE git rm -rf * cp -r /usr/share/openstack-tripleo-heat-templates/* . git add * git commit -m "Updates for USDPACKAGE" git checkout master git merge USDPACKAGE git checkout my-customizations git rebase master git status git add [resolved files] git rebase --continue git checkout my-customizations openstack overcloud deploy --templates /home/stack/templates/openstack-tripleo-heat-templates [OTHER OPTIONS] parameters: {% for role in roles %} {{role.name}}Count: description: Number of {{role.name}} nodes to deploy type: number default: {{role.CountDefault|default(0)}} {% endfor %} parameters: ControllerCount: description: Number of Controller nodes to deploy type: number default: 1 openstack overcloud deploy --templates -e /usr/share/openstack-tripleo-heat-templates/environments/network-isolation.yaml openstack overcloud deploy --templates /home/stack/tripleo-heat-templates -e /home/stack/tripleo-heat-templates/environments/network-isolation.yaml cd /usr/share/openstack-tripleo-heat-templates ./tools/process-templates.py -o ~/openstack-tripleo-heat-templates-rendered