Assign values to template variables

Updated at:

You can assign values to template variables to parameterize resource configurations in your templates.

Data types for template variables

Type

Description

string

The name of an ECS instance.

integer

The size of an ECS system disk.

boolean

Whether to disable deployment for a resource.

map

Key-value pairs for resource tags.

list

The IP address whitelist for an ECS instance.

list<map>

A list of ECS data disks.

charge type

The billing method for a resource.

Define custom variables

The following example uses an ECS template to show how to assign values to variables of different data types.

  1. Create an ECS template as shown in the following figure. For more information, see Create a personal template.image

  2. Define the following variables for the template. For more information, see Configure template variables.

    Parameter

    Value

    Default

    Quantity

    ${count}

    1

    Instance name

    ${name}

    ecs

    System disk size

    ${storage}

    40

    Data disks

    ${data_disk}

    []

    Tags

    ${tags}

    {"cadt":"uat"}

  3. After you complete the configuration, view the defined variables and their types, such as integer, string, list<map>, and map, on the My Solutions page. image

Define global template variables

The following example extends the preceding one by adding global variables.

  1. Create an ECS template as shown in the following figure. For more information, see Create a personal template.essss2

  2. Define the variables for the template. For more information, see Configure template variables. In the custom variable configuration panel, two variables are defined in the Global Variables section: ${sys} (Type: string, Default: erp) and ${storage} (Type: integer, Default: 100). The Custom Variables section organizes variables by region. Each custom variable includes a name, placeholder, type, default value, RefId, and property name. For example, the variable ${name1} has a placeholder of ${name1}_${sys}, is associated with RefId 1711695457, and has the property name instance_name. The variable ${disk2} has a placeholder of ${storage}, is associated with RefId 1711705942, and has the property name system_disk_size. You can reference global variables in placeholders of custom variables to reuse parameters.

Assign values using Java code

The following Java code example shows how to assign values to template variables and create an application:

variables = Maps.newHashMap();
// Set the instance name.
variables.put("${name}", "ecs123");
// Set the number of instances.
variables.put("${count}", "1");
// Set the system disk size.
variables.put("${storage}", "40");
// Set the data disks.
List<Map<String, String>> disks = Lists.newArrayList();
Map<String, String> disk1 = Maps.newHashMap();
disk1.put("name", "disk1");
disk1.put("size", "50");
disk1.put("category", "cloud_essd");
disk1.put("performance_level", "PL1");
disk1.put("encrypted", "false");
disks.add(disk1);
Map<String, String> disk2 = Maps.newHashMap();
disk2.put("name", "disk2");
disk2.put("size", "60");
disk2.put("category", "cloud_essd");
disk2.put("performance_level", "PL1");
disk2.put("encrypted", "false");
disks.add(disk2);
variables.put("${data_disk}", JSON.toJSONString(disks));
// Set the tags.
Map<String, String> tags = Maps.newHashMap();
tags.put("role", "dev");
tags.put("project", "abc");
variables.put("${tags}", JSON.toJSONString(tags));
// After you assign values to the variables, call the following API operation to create an application.
createApplicationRequest = new CreateApplicationRequest()
        .setTemplateId(templateId)      // The ID of the template that uses the variables.
        .setName(appName)                  // The application name.
        .setAreaId(region)                       // The region where the template is located.
        .setVariables(variables)              // Pass the variables.
        .setClientToken(UUID.randomUUID().toString());

Configure variables using a YAML file

Use the CADT command-line tool to call API operations and deploy CADT applications. A YAML configuration file defines the basic information required to create an application. For more information, see Usage notes of CADT command-line tool.