選擇ACS::Choice
更新時(shí)間:
用途
根據(jù)條件判斷結(jié)果來選擇接下來將執(zhí)行的任務(wù)。比如對(duì)一個(gè)實(shí)例重啟前,要查詢?cè)搶?shí)例的狀態(tài),根據(jù)狀態(tài)結(jié)果選擇將執(zhí)行的任務(wù),若該實(shí)例為運(yùn)行中,那么執(zhí)行對(duì)實(shí)例重啟的任務(wù);若該實(shí)例為已停止,執(zhí)行對(duì)實(shí)例啟動(dòng)的任務(wù),若以上狀態(tài)均不是,則執(zhí)行查詢實(shí)例狀態(tài)的任務(wù)。
語法
Tasks:
- Name: chooseNextTaskToRun # 任務(wù)名稱
Action: ACS::Choice
Properties:
DefaultTask: task1 #執(zhí)行完該chooseNextTaskToRun任務(wù),若無Choices中條件滿足時(shí),默認(rèn)將執(zhí)行名稱為task1的任務(wù)。task1不允許是對(duì)參數(shù)引用獲取的任務(wù)名,其必須是確定的字符串格式的任務(wù)名稱。
Choices:
- When: condition1 # 待判斷的條件1。
NextTask: task2 # 當(dāng)condition1為true時(shí),執(zhí)行完該chooseNextTaskToRun任務(wù),將執(zhí)行名稱為task2的任務(wù)。task2不允許是對(duì)參數(shù)引用獲取的任務(wù)名,其必須是確定的字符串格式的任務(wù)名稱。
- When: condition2 # 待判斷的條件2。
NextTask: task3 # 當(dāng)condition2為true時(shí),執(zhí)行完該chooseNextTaskToRun任務(wù),將執(zhí)行名稱為task3的任務(wù)。task3不允許是對(duì)參數(shù)引用獲取的任務(wù)名,其必須是確定的字符串格式的任務(wù)名稱。
# 如有需要,Choices中還可有多個(gè)When判斷條件。
#...
{
"Tasks": [
{
"Name": "chooseNextTaskToRun",
"Action": "ACS::Choice",
"Properties": {
"DefaultTask": "task1",
"Choices": [
{
"When": "condition1",
"NextTask": "task2"
},
{
"When": "condition2",
"NextTask": "task3"
}
]
}
}
]
}
示例
FormatVersion: OOS-2019-06-01
Description:
en: Bulky restarts the ECS instances.
zh-cn: 批量重啟ECS實(shí)例。
name-en: ACS-ECS-BulkyRebootInstances
name-zh-cn: 批量重啟ECS實(shí)例
Parameters:
targets:
Type: Json
AssociationProperty: Targets
AssociationPropertyMetadata:
ResourceType: 'ALIYUN::ECS::Instance::InstanceId'
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實(shí)例。
Action: 'ACS::SelectTargets'
Properties:
ResourceType: 'ALIYUN::ECS::Instance::InstanceId'
Filters:
- '{{ targets }}'
Outputs:
instanceIds:
Type: List
ValueSelector: 'Instances.Instance[].InstanceId'
status:
Type: String
ValueSelector: Instances.Instance[].Status
- Name: whetherStatusRunning
Action: 'ACS::Choice'
Description:
en: Starts the ECS instances
zh-cn: 啟動(dòng)實(shí)例
Properties:
DefaultTask: rebootInstance
Choices:
- When:
'Fn::Equals':
- Stopped
- '{{ describeInstances.status }}'
NextTask: startInstance
- Name: startInstance
Action: 'ACS::ECS::StartInstance'
Description:
en: Starts the ECS instances
zh-cn: 啟動(dòng)實(shí)例
Properties:
instanceId: '{{ ACS::TaskLoopItem }}'
Loop:
RateControl: '{{ rateControl }}'
Items: '{{ getInstance.instanceIds }}'
- Name: rebootInstance
Action: 'ACS::ECS::RebootInstance'
Description:
en: Restarts the ECS instances
zh-cn: 重啟實(shí)例
Properties:
instanceId: '{{ ACS::TaskLoopItem }}'
Loop:
RateControl: '{{ rateControl }}'
Items: '{{ getInstance.instanceIds }}'
{
"FormatVersion": "OOS-2019-06-01",
"Description": {
"en": "Bulky restarts the ECS instances.",
"zh-cn": "批量重啟ECS實(shí)例。",
"name-en": "ACS-ECS-BulkyRebootInstances",
"name-zh-cn": "批量重啟ECS實(shí)例"
},
"Parameters": {
"targets": {
"Type": "Json",
"AssociationProperty": "Targets",
"AssociationPropertyMetadata": {
"ResourceType": "ALIYUN::ECS::Instance"
}
},
"rateControl": {
"Description": {
"en": "Concurrency ratio of task execution.",
"zh-cn": "任務(wù)執(zhí)行的并發(fā)比率。"
},
"Type": "Json",
"AssociationProperty": "RateControl"
},
"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實(shí)例。"
},
"Action": "ACS::SelectTargets",
"Properties": {
"ResourceType": "ALIYUN::ECS::Instance",
"Filters": [
"{{ targets }}"
]
},
"Outputs": {
"instanceIds": {
"Type": "List",
"ValueSelector": "Instances.Instance[].InstanceId"
}
}
},
{
"Name": "describeInstances",
"Action": "ACS::ExecuteAPI",
"Description": "Views A ECS instances Status.",
"Properties": {
"Service": "ECS",
"API": "DescribeInstances",
"Parameters": {
"InstanceIds": "{{ getInstance.instanceIds }}"
}
},
"Outputs": {
"status": {
"Type": "String",
"ValueSelector": "Instances.Instance[].Status"
}
}
},
{
"Name": "whetherStatusRunning",
"Action": "ACS::Choice",
"Properties": {
"DefaultTask": "describeInstancesFinally",
"Choices": [
{
"When": {
"Fn::Equals": [
"Running",
"{{ describeInstances.status }}"
]
},
"NextTask": "rebootInstance"
},
{
"When": {
"Fn::Equals": [
"Stopped",
"{{ describeInstances.status }}"
]
},
"NextTask": "startInstance"
}
]
}
},
{
"Name": "startInstance",
"Action": "ACS::ECS::StartInstance",
"Description": {
"en": "Starts the ECS instances.",
"zh-cn": "啟動(dòng)實(shí)例。"
},
"Properties": {
"instanceId": "{{ ACS::TaskLoopItem }}"
},
"Loop": {
"RateControl": "{{ rateControl }}",
"Items": "{{ getInstance.instanceIds }}"
}
},
{
"Name": "rebootInstance",
"Action": "ACS::ECS::RebootInstance",
"Description": {
"en": "Restarts the ECS instances.",
"zh-cn": "重啟實(shí)例。"
},
"Properties": {
"instanceId": "{{ ACS::TaskLoopItem }}"
},
"Loop": {
"RateControl": "{{ rateControl }}",
"Items": "{{ getInstance.instanceIds }}"
}
},
{
"Name": "describeInstancesFinally",
"Action": "ACS::ExecuteAPI",
"Description": "Views the ECS instances Status.",
"Properties": {
"Service": "ECS",
"API": "DescribeInstances",
"Parameters": {
"InstanceIds": "{{ getInstance.instanceIds }}"
}
},
"Outputs": {
"status": {
"Type": "String",
"ValueSelector": "Instances.Instance[].Status"
}
}
}
],
"Outputs": {
"instanceIds": {
"Type": "List",
"Value": "{{ getInstance.instanceIds }}"
}
}
}
文檔內(nèi)容是否對(duì)您有幫助?