嵌套ACS::Template

调用其他的模板作为子模板进行嵌套执行。

用途

嵌套动作(ACS::Template)可以调用其他的模板作为子模板进行嵌套执行。利用该动作,您可以将一个复杂的模板进行模块化分解,拆分成多个子模板进行调用,也可以将常见的运维操作定义成一个可复用的子模板。另外云产品动作是OOS提供的常见类型云产品操作模板,您通过嵌套云产品动作可更便捷地进行嵌套调用。

语法

通过模板名称嵌套已创建过的模板

  • 通用格式(适用于所有可被嵌套模板)

    Tasks:
    - Name: callChildTemplate1
      Action: ACS::Template
      Properties:
        TemplateName: child_template_name # 必填,子模板名称
        TemplateVersion: v2 # 可选,默认取最新的版本。
        Parameters:  # 可选,子模板,根据子模板的参数提供参数。
          ParameterName1: value1
    {
      "Tasks": [
        {
          "Name": "callChildTemplate1",
          "Action": "ACS::Template",
          "Properties": {
            "TemplateName": "child_template_name",
            "TemplateVersion": "v2",
            "Parameters": {
              "ParameterName1": "value1"
            }
          }
        }
      ]
    }                                
  • 简化格式(仅适用于嵌套云产品动作)

    Tasks:
    - Name: taskName # 必填,任务名称,如runMyInstances
      Action: CloudProductActionName1  # 必填,云产品动作名称,如ACS::ECS::RunInstances
      Properties:
        ParameterName1: value1 # 可选,根据云产品动作需要的参数进行提供,如ImageId。
    {
      "Tasks": [
        {
          "Name": "taskName",
          "Action": "CloudProductActionName1",
          "Properties": {
            "ParameterName1": "parameterName1"
          }
        }
      ]
    }

通过模板内容嵌套未创建过的模板

Tasks:
- Name: callChildTemplate1
  Action: ACS::Template
  Properties:
    TemplateContent: 
      FormatVersion: OOS-2019-06-01 #必填,模板的版本。
      Parameters: #可选,子模板的参数。
      Tasks: #可选,子模板执行的动作。
{
  "Tasks": [
    {
      "Name": "callChildTemplate1",
      "Action": "ACS::Template",
      "Properties": {
        "TemplateContent": {
          "FormatVersion": "OOS-2019-06-01",
          "Parameters": null,
          "Tasks": null
        }
      }
    }
  ]
}

限制

嵌套动作有如下限制:

  • 模板不能嵌套自身。

  • 不能循环嵌套,即A嵌套B,B嵌套C,然后C嵌套A。

  • 总的嵌套层数不能超过3层。

  • 云产品动作属于一层嵌套,循环任务不属于嵌套。

示例

通过嵌套云产品动作ACS::ECS::CreateImage为ECS实例创建镜像

  • 通用定义格式(适用于所有可被嵌套模板)

    ---
    FormatVersion: OOS-2019-06-01
    Description: Creates a custom image.
    Parameters:
      imageName:
        Description: The image name.
        Type: String
      instanceId:
        Description: The ID of the instance.
        Type: String
        AllowedPattern: i-[A-Za-z0-9]*
        MinLength: 1
        MaxLength: 30
      OOSAssumeRole:
        Type: String
        Description: oos assume this role to execution task
        Default: OOSServiceRole
    RamRole: '{{OOSAssumeRole}}'
    Tasks:
    - Name: createImage
      Action: ACS::Template
      Description: Creates a custom image.
      Properties:
        TemplateName: 'ACS::ECS::CreateImage'
        Parameters:
          ImageName: '{{ imageName }}'
          InstanceId: '{{ instanceId }}'
      Outputs:
        imageId:
          Type: String
          ValueSelector: ImageId
    Outputs:
      imageId:
        Type: String
        Value: '{{ createImage.imageId }}'        
    {
        "FormatVersion": "OOS-2019-06-01",
        "Description": "Creates a custom image.",
        "Parameters": {
            "imageName": {
                "Description": "The image name.",
                "Type": "String"
            },
            "instanceId": {
                "Description": "The ID of the instance.",
                "Type": "String",
                "AllowedPattern": "i-[A-Za-z0-9]*",
                "MinLength": 1,
                "MaxLength": 30
            },
            "OOSAssumeRole": {
                "Type": "String",
                "Description": "oos assume this role to execution task",
                "Default": "OOSServiceRole"
            }
        },
        "RamRole": "{{OOSAssumeRole}}",
        "Tasks": [
            {
                "Name": "createImage",
                "Action": "ACS::Template",
                "Description": "Creates a custom image.",
                "Properties": {
                    "TemplateName": "ACS::ECS::CreateImage",
                    "Parameters": {
                        "ImageName": "{{ imageName }}",
                        "InstanceId": "{{ instanceId }}"
                    }
                },
                "Outputs": {
                    "imageId": {
                        "Type": "String",
                        "ValueSelector": "ImageId"
                    }
                }
            }
        ],
        "Outputs": {
            "imageId": {
                "Type": "String",
                "Value": "{{ createImage.imageId }}"
            }
        }
    }        
  • 简化定义格式(仅适用于云产品动作)

    ---
    FormatVersion: OOS-2019-06-01
    Description: Creates a new Image from existing ECS Instance.
    Parameters:
      InstanceId:
        Description: the Instance Type for the new instances
        Type: String
      ImageName:
        Description:  name of the new image
        Type: String
      OOSAssumeRole:
        Type: String
        Description: oos assume this role to execution task
        Default: OOSServiceRole
    RamRole: "{{OOSAssumeRole}}"
    Tasks:
      - Name: createImage
        Action: ACS::ECS::CreateImage
        Properties:
          ImageName: "{{ ImageName }}"
          InstanceId: "{{ InstanceId }}"
        Outputs:
          ImageId:
            ValueSelector: ImageId
            Type: String
    Outputs:
      ImageId:
        Type: String
        Value: "{{ createImage.ImageId }}"
    {
      "FormatVersion": "OOS-2019-06-01",
      "Description": "Creates a new Image from existing ECS Instance.",
      "Parameters": {
        "InstanceId": {
          "Description": "the Instance Type for the new instances",
          "Type": "String"
        },
        "ImageName": {
          "Description": "name of the new image",
          "Type": "String"
        },
        "OOSAssumeRole": {
          "Type": "String",
          "Description": "oos assume this role to execution task",
          "Default": "OOSServiceRole"
        }
      },
      "RamRole": "{{OOSAssumeRole}}",
      "Tasks": [
        {
          "Name": "createImage",
          "Action": "ACS::ECS::CreateImage",
          "Properties": {
            "ImageName": "{{ ImageName }}",
            "InstanceId": "{{ InstanceId }}"
          },
          "Outputs": {
            "ImageId": {
              "ValueSelector": "ImageId",
              "Type": "String"
            }
          }
        }
      ],
      "Outputs": {
        "ImageId": {
          "Type": "String",
          "Value": "{{ createImage.ImageId }}"
        }
      }
    }

使用TemplateContent查看指定标签的ECS实例

FormatVersion: OOS-2019-06-01
Description:
  name-en: DescribeInstances
  name-zh-cn: 通过指定标签查看ECS实例
Parameters:
  regionId:
    Label:
      en: The ID of region
      zh-cn: 地域ID
    Type: String
    Default: '{{ ACS::RegionId }}'
Tasks:
  - Name: describeInstances_out
    Action: ACS::Template
    Description:
      en: Queries the ECS instances by specifying tag
      zh-cn: 通过指定标签查看ECS实例
    Properties:
      TemplateContent:
        FormatVersion: OOS-2019-06-01
        Description: ''
        Parameters:
          regionId:
            Label:
              en: The ID of region
              zh-cn: 地域ID
            Type: String
            Default: '{{ ACS::RegionId }}'
        Tasks:
          - Name: describeInstances_1
            Action: ACS::Template
            Description:
              en: Queries the ECS instances by specifying tag
              zh-cn: 通过指定标签查看ECS实例
            Properties:
              TemplateContent:
                FormatVersion: OOS-2019-06-01
                Description: ''
                Parameters:
                  regionId:
                    Label:
                      en: The ID of region
                      zh-cn: 地域ID
                    Type: String
                    Default: '{{ ACS::RegionId }}'
                Tasks:
                  - Name: describeInstances_2
                    Action: ACS::ExecuteAPI
                    Description:
                      en: Queries the ECS instances by specifying tag
                      zh-cn: 通过指定标签查看ECS实例
                    Properties:
                      Service: ECS
                      API: DescribeInstances
                      Parameters:
                        RegionId: '{{ regionId }}'
                    Outputs:
                      instanceIdss:
                        Type: List
                        ValueSelector: Instances.Instance[].InstanceId
                Outputs:
                  instanceId1:
                    Type: List
                    Value: '{{ describeInstances_2.instanceIdss}}'
              Parameters:
                regionId: '{{ regionId }}'
            Outputs:
              instanceId2:
                Type: List
                ValueSelector: .instanceId1
        Outputs:
          instanceId1:
            Type: List
            Value: '{{ describeInstances_1.instanceId2 }}'
      Parameters:
        regionId: '{{ regionId }}'
    Outputs:
      instanceId2:
        Type: List
        ValueSelector: .instanceId1
Outputs:
  instanceId4:
    Type: List
    Value: '{{ describeInstances_out.instanceId2}}'
{
  "FormatVersion": "OOS-2019-06-01",
  "Description": {
    "name-en": "DescribeInstances",
    "name-zh-cn": "通过指定标签查看ECS实例"
  },
  "Parameters": {
    "regionId": {
      "Label": {
        "en": "The ID of region",
        "zh-cn": "地域ID"
      },
      "Type": "String",
      "Default": "{{ ACS::RegionId }}"
    }
  },
  "Tasks": [
    {
      "Name": "describeInstances_out",
      "Action": "ACS::Template",
      "Description": {
        "en": "Queries the ECS instances by specifying tag",
        "zh-cn": "通过指定标签查看ECS实例"
      },
      "Properties": {
        "TemplateContent": {
          "FormatVersion": "OOS-2019-06-01",
          "Description": "",
          "Parameters": {
            "regionId": {
              "Label": {
                "en": "The ID of region",
                "zh-cn": "地域ID"
              },
              "Type": "String",
              "Default": "{{ ACS::RegionId }}"
            }
          },
          "Tasks": [
            {
              "Name": "describeInstances_1",
              "Action": "ACS::Template",
              "Description": {
                "en": "Queries the ECS instances by specifying tag",
                "zh-cn": "通过指定标签查看ECS实例"
              },
              "Properties": {
                "TemplateContent": {
                  "FormatVersion": "OOS-2019-06-01",
                  "Description": "",
                  "Parameters": {
                    "regionId": {
                      "Label": {
                        "en": "The ID of region",
                        "zh-cn": "地域ID"
                      },
                      "Type": "String",
                      "Default": "{{ ACS::RegionId }}"
                    }
                  },
                  "Tasks": [
                    {
                      "Name": "describeInstances_2",
                      "Action": "ACS::ExecuteAPI",
                      "Description": {
                        "en": "Queries the ECS instances by specifying tag",
                        "zh-cn": "通过指定标签查看ECS实例"
                      },
                      "Properties": {
                        "Service": "ECS",
                        "API": "DescribeInstances",
                        "Parameters": {
                          "RegionId": "{{ regionId }}"
                        }
                      },
                      "Outputs": {
                        "instanceIdss": {
                          "Type": "List",
                          "ValueSelector": "Instances.Instance[].InstanceId"
                        }
                      }
                    }
                  ],
                  "Outputs": {
                    "instanceId1": {
                      "Type": "List",
                      "Value": "{{ describeInstances_2.instanceIdss}}"
                    }
                  }
                },
                "Parameters": {
                  "regionId": "{{ regionId }}"
                }
              },
              "Outputs": {
                "instanceId2": {
                  "Type": "List",
                  "ValueSelector": ".instanceId1"
                }
              }
            }
          ],
          "Outputs": {
            "instanceId1": {
              "Type": "List",
              "Value": "{{ describeInstances_1.instanceId2 }}"
            }
          }
        },
        "Parameters": {
          "regionId": "{{ regionId }}"
        }
      },
      "Outputs": {
        "instanceId2": {
          "Type": "List",
          "ValueSelector": ".instanceId1"
        }
      }
    }
  ],
  "Outputs": {
    "instanceId4": {
      "Type": "List",
      "Value": "{{ describeInstances_out.instanceId2}}"
    }
  }
}