Description
The ACS::Choice action can be used to determine the task to be run based on the judgment of the conditions For example, before you restart an instance, you want to query the status of the instance and select the task to be run based on the returned result. If the instance is in the Running state, the restart task is run. If the instance is in the Stopped state, the start task is run. If the instance is in other states, the query task is run.
Syntax
- YAML format
Tasks:
- Name: chooseNextTaskToRun # The name of the task.
Action: ACS::Choice
Properties:
DefaultTask: task1 # The default task that is run if no condition specified by Choices is met after the chooseNextTaskToRun task is run. The name of the task1 task cannot be the name of a parameter. It must be a certain string.
Choices:
- When: condition1 # The first condition for judgment.
NextTask: task2 # The task that is run if the first condition is met.
- When: condition2 # The second condition for judgment.
NextTask: task3 # The task that is run if the second condition is met.
# If necessary, you can use more WHEN conditions in the command.
#...
- JSON format (For more information, see the parameter description for the YAML format.)
{
"Tasks": [{
"Name": "chooseNextTaskToRun",
"Action": "ACS::Choice",
"Properties": {
"DefaultTask": "task1",
"Choices": [
{
"When": "condition1",
"NextTask": "task2"
},
{
"When": "condition2",
"NextTask": "task3"
}]}}]
}
Examples
- YAML format
FormatVersion: OOS-2019-06-01
Description:
en: Bulky restarts the ECS instances.
null-null-null
name-en: ACS-ECS-BulkyRebootInstances
null-null-null
Parameters:
targets:
Type: Json
AssociationProperty: Targets
AssociationPropertyMetadata:
ResourceType: 'ALIYUN::ECS::Instance'
rateControl:
Description:
en: Concurrency ratio of task execution.
null-null-null
Type: Json
AssociationProperty: RateControl
OOSAssumeRole:
Description:
en: The RAM role to be assumed by OOS.
null-null-null
Type: String
Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
- Name: getInstance
Description:
en: Views the ECS instances.
null-null-null
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.
null-null-null
Properties:
instanceId: '{{ ACS::TaskLoopItem }}'
Loop:
RateControl: '{{ rateControl }}'
Items: '{{ getInstance.instanceIds }}'
- Name: rebootInstance
Action: 'ACS::ECS::RebootInstance'
Description:
en: Restarts the ECS instances.
null-null-null
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 }}'
- JSON format
{
"FormatVersion": "OOS-2019-06-01",
"Description": {
"en": "Bulky restarts the ECS instances.",
null-null-null
"name-en": "ACS-ECS-BulkyRebootInstances",
null-null-null
},
"Parameters": {
"targets": {
"Type": "Json",
"AssociationProperty": "Targets",
"AssociationPropertyMetadata": {
"ResourceType": "ALIYUN::ECS::Instance"
}
},
"rateControl": {
"Description": {
"en": "Concurrency ratio of task execution.",
null-null-null
},
"Type": "Json",
"AssociationProperty": "RateControl"
},
"OOSAssumeRole": {
"Description": {
"en": "The RAM role to be assumed by OOS.",
null-null-null
},
"Type": "String",
"Default": "OOSServiceRole"
}
},
"RamRole": "{{ OOSAssumeRole }}",
"Tasks": [
{
"Name": "getInstance",
"Description": {
"en": "Views the ECS instances.",
null-null-null
},
"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.",
null-null-null
},
"Properties": {
"instanceId": "{{ ACS::TaskLoopItem }}"
},
"Loop": {
"RateControl": "{{ rateControl }}",
"Items": "{{ getInstance.instanceIds }}"
}
},
{
"Name": "rebootInstance",
"Action": "ACS::ECS::RebootInstance",
"Description": {
"en": "Restarts the ECS instances.",
null-null-null
},
"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 }}"
}}}