选择目标资源ACS::SelectTargets

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

用途

如果您在模版中使用了ACS::SelectTargets动作,并定义了包含"AssociationProperty": "Targets"的模版参数,当您创建执行来设置参数时,可轻易切换批量选取资源的方式。有手动选择资源、指定资源的标签、选择全部资源、CSV文件(包含资源ID)和指定配置清单条件几种方式,方便您快速完成资源的批量选择。

注意

当前该动作支持的资源类型仅有ECS实例。

selecttargets

语法

  • 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 }}"
    }
  }
}

不同实例选择方式targets参数示例

  • 根据实例ID选择(YAML)

targets:
  Type: ResourceIds
  RegionId: cn-hangzhou
  ResourceIds:
    - i-******************1
    - i-******************2
    - i-******************3

  • 根据实例ID选择(JSON)

"targets": {
  "Type": "ResourceIds",
  "RegionId": "cn-hangzhou",
  "ResourceIds": [
    "i-******************1",
    "i-******************2",
    "i-******************3"
  ]
}

  • 根据标签选择(YAML)

targets:
  Type: Tags
  RegionId: cn-hangzhou
  Tags:
    - Key: key1
      Value: value1
    - Key: key2
      Value: value2

  • 根据标签选择(JSON)

"targets": {
  "Type": "Tags",
  "RegionId": "cn-hangzhou",
  "Tags": [
    {
      "Key": "key1",
      "Value": "value1"
    },
    {
      "Key": "key2",
      "Value": "value2"
    }
  ]
}

  • 根据资源组选择(YAML)

targets:
  Type: ResourceGroup
  RegionId: cn-hangzhou
  ResourceGroupId: rg-acf*********re3y

  • 根据资源组选择(JSON)

"targets": {
    "Type": "ResourceGroup",
    "RegionId": "cn-hangzhou",
    "ResourceGroupId": "rg-acf********re3y"
}

  • 选择所有实例(YAML)

targets:
    Type: All
    RegionId: cn-hangzhou
    Parameters:
      RegionId: cn-hangzhou
      Status: Running
      Tags:
        - Key: key1
          Value: value1

  • 选择所有实例(JSON)

"targets": {
    "Type": "All",
    "RegionId": "cn-hangzhou",
    "Parameters": {
      "RegionId": "cn-hangzhou",
      "Status": "Running",
      "Tags": [
        {
          "Key": "key1",
          "Value": "value1"
        }
      ]
    }
  }

  • 根据指定配置清单条件选择(YAML)

targets:
    Type: Inventory
    RegionId: cn-hangzhou
    InventoryFilters:
      - Name: TypeName
        Operator: Equal
        Value:
          - ACS:InstanceInformation
      - Name: ACS:InstanceInformation.Status
        Value:
          - Running
        Operator: Equal

  • 根据指定配置清单条件选择(JSON)

"targets": {
      "Type": "Inventory",
      "RegionId": "cn-hangzhou",
      "InventoryFilters": [
        {
          "Name": "TypeName",
          "Operator": "Equal",
          "Value": [
            "ACS:InstanceInformation"
          ]
        },
        {
          "Name": "ACS:InstanceInformation.Status",
          "Value": [
            "Running"
          ],
          "Operator": "Equal"
        }
      ]
    }