选择ACS::Choice

更新时间: 2023-10-16 14:28:04

用途

根据条件判断结果来选择接下来将执行的任务。比如对一个实例重启前,要查询该实例的状态,根据状态结果选择将执行的任务,若该实例为运行中,那么执行对实例重启的任务;若该实例为已停止,执行对实例启动的任务,若以上状态均不是,则执行查询实例状态的任务。

语法

Tasks:
- Name: chooseNextTaskToRun # 任务名称
  Action: ACS::Choice
  Properties:
    DefaultTask:  task1  #执行完该chooseNextTaskToRun任务,若无Choices中条件满足时,默认将执行名称为task1的任务。task1不允许是对参数引用获取的任务名,其必须是确定的字符串格式的任务名称。
    Choices:
      - When: condition1  # 待判断的条件1。
        NextTask: task2  # 当condition1为true时,执行完该chooseNextTaskToRun任务,将执行名称为task2的任务。task2不允许是对参数引用获取的任务名,其必须是确定的字符串格式的任务名称。
      - When: condition2 # 待判断的条件2。
        NextTask: task3 # 当condition2为true时,执行完该chooseNextTaskToRun任务,将执行名称为task3的任务。task3不允许是对参数引用获取的任务名,其必须是确定的字符串格式的任务名称。
                # 如有需要,Choices中还可有多个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实例。
  name-en: ACS-ECS-BulkyRebootInstances
  name-zh-cn: 批量重启ECS实例
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实例。
    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: 启动实例
    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: 启动实例
    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 }}'
{
    "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 }}"
        }
    }
}
阿里云首页 系统运维管理 相关技术圈