All Products
Search
Document Center

CloudOps Orchestration Service:ACS::Choice

Last Updated:Feb 10, 2023

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

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.
        #...
{
    "Tasks": [
        {
            "Name": "chooseNextTaskToRun",
            "Action": "ACS::Choice",
            "Properties": {
                "DefaultTask": "task1",
                "Choices": [
                    {
                        "When": "condition1",
                        "NextTask": "task2"
                    },
                    {
                        "When": "condition2",
                        "NextTask": "task3"
                    }
                ]
            }
        }
    ]
}

Examples

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 }}'
{
    "FormatVersion": "OOS-2019-06-01",
    "Description": {
        "en": "Bulky restarts the ECS instances.",
        "zh-cn": "批量重启ECS实例。",
        "name-en": "ACS-ECS-BulkyRebootInstances",
        "name-zh-cn": "批量重启ECS实例"
    },
    "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"
        },
        "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": "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": "启动实例。"
            },
            "Properties": {
                "instanceId": "{{ ACS::TaskLoopItem }}"
            },
            "Loop": {
                "RateControl": "{{ rateControl }}",
                "Items": "{{ getInstance.instanceIds }}"
            }
        },
        {
            "Name": "rebootInstance",
            "Action": "ACS::ECS::RebootInstance",
            "Description": {
                "en": "Restarts the ECS instances.",
                "zh-cn": "重启实例。"
            },
            "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 }}"
        }
    }
}