使用该动作,当设置参数时,批量选择资源将会非常方便。

用途

如果您在模版中使用了ACS::SelectTargets动作,并定义了包含"AssociationProperty": "Targets"的模版参数,当您创建执行来设置参数时,可轻易切换批量选取资源的方式,不管是通过手动选择资源实例指定资源标签指定实例资源组还是直接上传CSV文件(包含资源ID)都是被支持的,可方便您快速完成资源的批量选择。对于CSV文件可通过实例列表中的导出资源列表获得,并确保每行内容以“,“结尾作为分隔符。
注意 当前该动作支持的资源类型仅有ECS实例。

语法

  • YAML格式
FormatVersion: OOS-2019-06-01
Description: 'ACS::SelectTargets usage'
Parameters:
  targets:  # 参数名,用来选择资源范围。
    Type: Json  # 参数类型,固定为Json。
    AssociationProperty: Targets    # 参数关联的动作,对应ACS::SelectTargets时,固定为Targets 。
    AssociationPropertyMetadata:  # 参数要选择的资源定义。
      ResourceType: 'ALIYUN::ECS::Instance' # 要选择的资源类型,资源为ECS实例时,固定为ALIYUN::ECS::Instance。
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的返回结果。
  • JSON格式(请参照YAML注释说明)
{
  "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实例(YAML)
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实例(JSON)
{
  "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 }}"
    }
  }
}