使用該動作,當設置參數時,批量選擇資源將會非常方便。
用途
如果您在模板中使用了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 資源為ECD云桌面
# 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" } } }
文檔內容是否對您有幫助?