This topic describes how to specify values for custom template variables.
Data types of template variables
Data type | Description |
String | Elastic Compute Service (ECS) instance name. |
Integer | ECS system disk capacity. |
Boolean | Whether to disable deployment for a resource. |
Map | Resource tag information. |
List | IP address whitelist of ECS instances. |
List<Map> | ECS data disk list. |
ChargeType | Only applicable to the Charge type variable. |
Define local template variables
In this example, an ECS template is used to describe how to specify values for different types of variables.
Create an ECS template. The following figure shows an example. For more information, see Create a custom template.
Define the following variables for the template. For more information, see Configure template variables.
Variable
Variable value
Default value
count
${count}
1
instance_name
${name}
ecs
system_disk_size
${storage}
40
data_disks
${data_disk}
[]
tags
${tags}
{"cadt":"uat"}
After you complete the settings, find the template on the My Solutions page to view the defined variables. You can see that the variable types include Integer, String, List<Map>, and Map.
Specify values for template variables by using Java code
If you are familiar with the Java programming language, you can use Java code to specify values for template variables. The following sample code provides an example:
variables = Maps.newHashMap();
// Specify a value for the ${name} variable.
variables.put("${name}", "ecs123");
// Specify a value for the ${count} variable.
variables.put("${count}", "1");
// Specify a value for the ${storage} variable.
variables.put("${storage}", "40");
// Specify a value for the ${data_disk} variable.
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));
// Specify a value for the ${tags} variable.
Map<String, String> tags = Maps.newHashMap();
tags.put("role", "dev");
tags.put("project", "abc");
variables.put("${tags}", JSON.toJSONString(tags));
// After you specify values for 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 ID of the application.
.setAreaId(region) // The region in which the template resides.
.setVariables(variables) // Pass the variables.
.setClientToken(UUID.randomUUID().toString());
Define global template variables
In this example, an ECS template is used. The following section describes how to configure template variables.
Create an ECS template. The following figure shows an example. For more information, see Create a custom template.
Define the following variables for the template. For more information, see Configure template variables.
Specify values for template variables by using Java code
variables = Maps.newHashMap();
// Specify a value for the ${name1} variable.
variables.put("${name1}", "ecs001");
// Specify a value for the ${name2} variable.
variables.put("${name2}", "ecs002");
// Specify a value for the ${sys} variable.
variables.put("${sys}", "erp");
// Specify a value for the ${storage} variable.
variables.put("${storage}", "80");
// After you specify values for 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 ID of the application.
.setAreaId(region) // The region in which the template resides.
.setVariables(variables) // Pass the variables.
.setClientToken(UUID.randomUUID().toString());
After you run the code, the names of the ECS instances are ecs001_erp and ecs002-erp. The system disk capacity of both the ECS instances is 80 GB.