全部產品
Search
文件中心

CloudOps Orchestration Service:嵌套ACS::Template

更新時間:Sep 30, 2024

調用其他的模板作為子模板進行嵌套執行。

用途

嵌套動作(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}}"
    }
  }
}