使用該動作,當設定參數時,批量選擇資源將會非常方便。
用途
如果您在模板中使用了ACS::SelectTargets動作,並定義了包含"AssociationProperty": "Targets"的模板參數,當您建立執行來設定參數時,可輕易切換批量選取資源的方式。有手動選擇資源、指定資源的標籤、選擇全部資源、CSV檔案(包含資源ID)和指定配置清單條件幾種方式,方便您快速完成資源的批量選擇。
文法
FormatVersion: OOS-2019-06-01
Description: 'ACS::SelectTargets usage'
Parameters:
targets: # 參數名,用來選擇資源範圍。
Type: Json # 參數類型,固定為Json。
AssociationProperty: Targets # 參數關聯的動作,對應ACS::SelectTargets時,固定為Targets 。
AssociationPropertyMetadata: # 參數要選擇的資源定義。
ResourceType: ALIYUN::ECS::Instance # 要選擇的資源類型。
# ALIYUN::ECS::Instance 資源為ECS執行個體
# ALIYUN::ECD::Desktop 資源為ECDCloudDesktop
# ALIYUN::RDS::Instance 資源為RDS執行個體
# ALIYUN::Redis::Instance 資源為REDIS執行個體
# ALIYUN::MongoDB::Instance 資源為MongoDB執行個體
# ALIYUN::ECS::ManagedInstance 資源為ECS受管理的執行個體
# ALIYUN::ComputeNestSupplier::Instance 資源為ComputeNestSupplier執行個體
Tasks:
- Name: getInstance # 任務名稱。
Action: 'ACS::SelectTargets' # 確定選擇具體資源的動作。
Properties:
ResourceType: 'ALIYUN::ECS::Instance' # 要選擇的資源類型。
Filters: # 要篩選的範圍。
- '{{ targets }}' # 選擇資源範圍的參數名。
Outputs: # 任務輸出。
instanceIds: # 任務輸出參數名。
Type: List # 輸出參數類型。
ValueSelector: 'Instances.Instance[].InstanceId' # jq Selector,其篩選的對象為該資源類型的Describe OpenAPI返回結果,比如ECS執行個體的篩選對象則是DescribeInstances OpenAPI的返回結果。
{
"FormatVersion": "OOS-2019-06-01",
"Description": "ACS::SelectTargets usage",
"Parameters": {
"targets": {
"Type": "Json",
"AssociationProperty": "Targets",
"AssociationPropertyMetadata": {
"ResourceType": "ALIYUN::ECS::Instance"
}
}
},
"Tasks": [
{
"Name": "getInstance",
"Action": "ACS::SelectTargets",
"Properties": {
"ResourceType": "ALIYUN::ECS::Instance",
"Filters": [
"{{ targets }}"
]
},
"Outputs": {
"instanceIds": {
"Type": "List",
"ValueSelector": "Instances.Instance[].InstanceId"
}
}
}
]
}
模板樣本
大量啟動ECS執行個體
FormatVersion: OOS-2019-06-01 Description: en: Bulky starts the ECS instances. zh-cn: 大量啟動ECS執行個體。 name-en: ACS-ECS-BulkyStartInstances name-zh-cn: 大量啟動ECS執行個體 categories: - instance_manage Parameters: targets: Type: Json AssociationProperty: Targets AssociationPropertyMetadata: ResourceType: 'ALIYUN::ECS::Instance' rateControl: Description: en: Concurrency ratio of task execution. zh-cn: 任務執行的並發比率。 Type: Json AssociationProperty: RateControl Default: Mode: Concurrency MaxErrors: 0 Concurrency: 10 OOSAssumeRole: Description: en: The RAM role to be assumed by OOS. zh-cn: OOS扮演的RAM角色。 Type: String Default: OOSServiceRole RamRole: '{{ OOSAssumeRole }}' Tasks: - Name: getInstance Description: en: Views the ECS instances. zh-cn: 擷取ECS執行個體。 Action: 'ACS::SelectTargets' Properties: ResourceType: 'ALIYUN::ECS::Instance' Filters: - '{{ targets }}' Outputs: instanceIds: Type: List ValueSelector: 'Instances.Instance[].InstanceId' - Name: startInstance Action: 'ACS::ECS::StartInstance' Description: en: Starts the ECS instances. zh-cn: 啟動執行個體。 Properties: instanceId: '{{ ACS::TaskLoopItem }}' Loop: RateControl: '{{ rateControl }}' Items: '{{ getInstance.instanceIds }}' Outputs: instanceIds: Type: List Value: '{{ getInstance.instanceIds }}'
{ "FormatVersion": "OOS-2019-06-01", "Description": { "en": "Bulky starts the ECS instances.", "zh-cn": "大量啟動ECS執行個體。", "name-en": "ACS-ECS-BulkyStartInstances", "name-zh-cn": "大量啟動ECS執行個體", "categories": [ "instance_manage" ] }, "Parameters": { "targets": { "Type": "Json", "AssociationProperty": "Targets", "AssociationPropertyMetadata": { "ResourceType": "ALIYUN::ECS::Instance" } }, "rateControl": { "Description": { "en": "Concurrency ratio of task execution.", "zh-cn": "任務執行的並發比率。" }, "Type": "Json", "AssociationProperty": "RateControl", "Default": { "Mode": "Concurrency", "MaxErrors": 0, "Concurrency": 10 } }, "OOSAssumeRole": { "Description": { "en": "The RAM role to be assumed by OOS.", "zh-cn": "OOS扮演的RAM角色。" }, "Type": "String", "Default": "OOSServiceRole" } }, "RamRole": "{{ OOSAssumeRole }}", "Tasks": [ { "Name": "getInstance", "Description": { "en": "Views the ECS instances.", "zh-cn": "擷取ECS執行個體。" }, "Action": "ACS::SelectTargets", "Properties": { "ResourceType": "ALIYUN::ECS::Instance", "Filters": [ "{{ targets }}" ] }, "Outputs": { "instanceIds": { "Type": "List", "ValueSelector": "Instances.Instance[].InstanceId" } } }, { "Name": "startInstance", "Action": "ACS::ECS::StartInstance", "Description": { "en": "Starts the ECS instances.", "zh-cn": "啟動執行個體。" }, "Properties": { "instanceId": "{{ ACS::TaskLoopItem }}" }, "Loop": { "RateControl": "{{ rateControl }}", "Items": "{{ getInstance.instanceIds }}" } } ], "Outputs": { "instanceIds": { "Type": "List", "Value": "{{ getInstance.instanceIds }}" } } }
不同ECS執行個體選擇方式targets參數樣本
根據執行個體ID選擇
targets: Type: ResourceIds RegionId: cn-hangzhou ResourceIds: - i-******************1 - i-******************2 - i-******************3
{ "targets": { "Type": "ResourceIds", "RegionId": "cn-hangzhou", "ResourceIds": [ "i-******************1", "i-******************2", "i-******************3" ] } }
根據標籤選擇
targets: Type: Tags RegionId: cn-hangzhou Tags: - Key: key1 Value: value1 - Key: key2 Value: value2
{ "targets": { "Type": "Tags", "RegionId": "cn-hangzhou", "Tags": [ { "Key": "key1", "Value": "value1" }, { "Key": "key2", "Value": "value2" } ] } }
根據資源群組選擇
targets: Type: ResourceGroup RegionId: cn-hangzhou ResourceGroupId: rg-acf*********re3y
{ "targets": { "Type": "ResourceGroup", "RegionId": "cn-hangzhou", "ResourceGroupId": "rg-acf*********re3y" } }
選擇所有執行個體
targets: Type: All RegionId: cn-hangzhou Parameters: RegionId: cn-hangzhou Status: Running Tags: - Key: key1 Value: value1
{ "targets": { "Type": "All", "RegionId": "cn-hangzhou", "Parameters": { "RegionId": "cn-hangzhou", "Status": "Running", "Tags": [ { "Key": "key1", "Value": "value1" } ] } } }
根據指定配置清單條件選擇
targets: Type: Inventory RegionId: cn-hangzhou InventoryFilters: - Name: TypeName Operator: Equal Value: - ACS:InstanceInformation - Name: ACS:InstanceInformation.Status Value: - Running Operator: Equal
{ "targets": { "Type": "Inventory", "RegionId": "cn-hangzhou", "InventoryFilters": [ { "Name": "TypeName", "Operator": "Equal", "Value": [ "ACS:InstanceInformation" ] }, { "Name": "ACS:InstanceInformation.Status", "Value": [ "Running" ], "Operator": "Equal" } ] } }
選擇計算巢服務執行個體Targets參數樣本
擷取計算巢服務執行個體模板樣本
FormatVersion: OOS-2019-06-01 Parameters: RegionId: Type: String Label: en: RegionId zh-cn: 地區ID AssociationProperty: RegionId Default: '{{ ACS::RegionId }}' Targets: Type: Json Label: en: TargetInstance zh-cn: 目標執行個體 AssociationProperty: Targets AssociationPropertyMetadata: ResourceType: ALIYUN::ComputeNestSupplier::Instance RegionId: RegionId RateControl: Label: en: RateControl zh-cn: 任務執行的並發比率 Type: Json AssociationProperty: RateControl Default: Mode: Concurrency MaxErrors: 0 Concurrency: 1 OOSAssumeRole: Label: en: OOSAssumeRole zh-cn: OOS扮演的RAM角色 Type: String RamRole: '{{ OOSAssumeRole }}' Tasks: - Name: GetServiceInstance Description: en: Views the compute nest supplier instances zh-cn: 計算巢商家側擷取服務執行個體 Action: ACS::SelectTargets Properties: ResourceType: ALIYUN::ComputeNestSupplier::Instance RegionId: '{{ RegionId }}' Filters: - '{{ Targets }}' Outputs: InstanceIds: Type: List ValueSelector: ServiceInstances[].ServiceInstanceId Metadata: ALIYUN::OOS::Interface: ParameterGroups: - Parameters: - RegionId - Targets Label: default: zh-cn: 配置參數 en: Configure Parameters - Parameters: - RateControl - OOSAssumeRole Label: default: zh-cn: 進階選項 en: Control Options
{ "FormatVersion": "OOS-2019-06-01", "Parameters": { "RegionId": { "Type": "String", "Label": { "en": "RegionId", "zh-cn": "地區ID" }, "AssociationProperty": "RegionId", "Default": "{{ ACS::RegionId }}" }, "Targets": { "Type": "Json", "Label": { "en": "TargetInstance", "zh-cn": "目標執行個體" }, "AssociationProperty": "Targets", "AssociationPropertyMetadata": { "ResourceType": "ALIYUN::ComputeNestSupplier::Instance", "RegionId": "RegionId" } }, "RateControl": { "Label": { "en": "RateControl", "zh-cn": "任務執行的並發比率" }, "Type": "Json", "AssociationProperty": "RateControl", "Default": { "Mode": "Concurrency", "MaxErrors": 0, "Concurrency": 1 } }, "OOSAssumeRole": { "Label": { "en": "OOSAssumeRole", "zh-cn": "OOS扮演的RAM角色" }, "Type": "String" } }, "RamRole": "{{ OOSAssumeRole }}", "Tasks": [ { "Name": "GetServiceInstance", "Description": { "en": "Views the compute nest supplier instances", "zh-cn": "計算巢商家側擷取服務執行個體" }, "Action": "ACS::SelectTargets", "Properties": { "ResourceType": "ALIYUN::ComputeNestSupplier::Instance", "RegionId": "{{ RegionId }}", "Filters": [ "{{ Targets }}" ] }, "Outputs": { "InstanceIds": { "Type": "List", "ValueSelector": "ServiceInstances[].ServiceInstanceId" } } } ], "Metadata": { "ALIYUN::OOS::Interface": { "ParameterGroups": [ { "Parameters": [ "RegionId", "Targets" ], "Label": { "default": { "zh-cn": "配置參數", "en": "Configure Parameters" } } }, { "Parameters": [ "RateControl", "OOSAssumeRole" ], "Label": { "default": { "zh-cn": "進階選項", "en": "Control Options" } } } ] } } }
根據執行個體ID選擇參數樣本
targets: ResourceIds: - si-9b136xxxxxxxxxxxxxxx - si-f63edxxxxxxxxxxxxxxx RegionId: cn-hangzhou Type: ResourceIds
{ "targets": { "ResourceIds": [ "si-9b1363ae63664f19a156", "si-f63ed519cc3445eebdcd" ], "RegionId": "cn-hangzhou", "Type": "ResourceIds" } }
選擇杭州地區服務Id為“"service-xxxx”版本為“4”且狀態為已部署的全部服務執行個體
targets: Type: All RegionId: cn-hangzhou Parameters: Filter: - Name: Status Value: - Deployed - Name: 'Version ' Value: - '4' - Name: ServiceId Value: - ervice-ea89c844c3e9430a9b9c RegionId: cn-hangzhou
{ "targets": { "Type": "All", "RegionId": "cn-hangzhou", "Parameters": { "Filter": [{ "Name": "Status", "Value": [ "Deployed" ] }, { "Name": "Version ", "Value": ["4"] }, { "Name": "ServiceId", "Value": ["ervice-ea89c844c3e9430a9b9c"] } ], "RegionId": "cn-hangzhou" } } }