全部产品
云市场

选择目标资源ACS::SelectTargets

更新时间:2019-12-03 10:37:12

用途

创建OOS执行前,在设置参数步骤时,如果被设置的参数为对资源的选择,通常可选择资源的路径有两种,一种是资源ID,另一种是资源的标签。如果用来选择资源的参数被定义为普通模版参数,当选择资源时,要么只能通过资源ID选择,要么就只能通过资源标签选择,除非重写模版,否则无法轻易切换选择资源的路径。
如果您在模版中配合参数使用了ACS::SelectTargets动作,当您在设置参数时,则可轻易切换选择资源的路径,无需更改模版内容,就可以通过资源的标签或资源的ID来选择资源。假设资源为ECS实例,当您设置的参数是对ECS实例的选择时,可使用Tag选择该Tag下的实例,也可直接指定具体实例。

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

语法

  • YAML格式
  1. FormatVersion: OOS-2019-06-01
  2. Description: 'ACS::SelectTargets usage'
  3. Parameters:
  4. targets: # 参数名,用来选择资源范围。
  5. Type: Json # 参数类型,固定为Json。
  6. AssociationProperty: Targets # 参数关联的动作,对应ACS::SelectTargets时,固定为Targets 。
  7. AssociationPropertyMetadata: # 参数要选择的资源定义。
  8. ResourceType: 'ALIYUN::ECS::Instance' # 要选择的资源类型,资源为ECS实例时,固定为'ALIYUN::ECS::Instance'。
  9. Tasks:
  10. - Name: getInstance # 任务名称。
  11. Action: 'ACS::SelectTargets' # 确定选择具体资源的动作。
  12. Properties:
  13. ResourceType: 'ALIYUN::ECS::Instance' # 要选择的资源类型。
  14. Filters: # 要筛选的范围。
  15. - '{{ targets }}' # 选择资源范围的参数名。
  16. Outputs: # 任务输出。
  17. instanceIds: # 任务输出参数名。
  18. Type: List # 输出参数类型。
  19. ValueSelector: 'Instances.Instance[].InstanceId' # jq Selector,其筛选的对象为该资源类型的Describe OpenAPI返回结果,比如ECS实例的筛选对象则是DescribeInstances OpenAPI的返回结果。
  • JSON格式(请参照YAML注释说明)
  1. {
  2. "FormatVersion": "OOS-2019-06-01",
  3. "Description": "ACS::SelectTargets usage",
  4. "Parameters": {
  5. "targets": {
  6. "Type": "Json",
  7. "AssociationProperty": "Targets",
  8. "AssociationPropertyMetadata": {
  9. "ResourceType": "ALIYUN::ECS::Instance"
  10. }
  11. }
  12. },
  13. "Tasks": [
  14. {
  15. "Name": "getInstance",
  16. "Action": "ACS::SelectTargets",
  17. "Properties": {
  18. "ResourceType": "ALIYUN::ECS::Instance",
  19. "Filters": [
  20. "{{ targets }}"
  21. ]
  22. },
  23. "Outputs": {
  24. "instanceIds": {
  25. "Type": "List",
  26. "ValueSelector": "Instances.Instance[].InstanceId"
  27. }
  28. }
  29. }
  30. ]
  31. }

示例

  • 批量启动ECS实例(YAML)
  1. FormatVersion: OOS-2019-06-01
  2. Description:
  3. en: Bulky starts the ECS instances.
  4. zh-cn: 批量启动ECS实例。
  5. name-en: ACS-ECS-BulkyStartInstances
  6. name-zh-cn: 批量启动ECS实例
  7. categories:
  8. - instance_manage
  9. Parameters:
  10. targets:
  11. Type: Json
  12. AssociationProperty: Targets
  13. AssociationPropertyMetadata:
  14. ResourceType: 'ALIYUN::ECS::Instance'
  15. rateControl:
  16. Description:
  17. en: Concurrency ratio of task execution.
  18. zh-cn: 任务执行的并发比率。
  19. Type: Json
  20. AssociationProperty: RateControl
  21. Default:
  22. Mode: Concurrency
  23. MaxErrors: 0
  24. Concurrency: 10
  25. OOSAssumeRole:
  26. Description:
  27. en: The RAM role to be assumed by OOS.
  28. zh-cn: OOS扮演的RAM角色。
  29. Type: String
  30. Default: OOSServiceRole
  31. RamRole: '{{ OOSAssumeRole }}'
  32. Tasks:
  33. - Name: getInstance
  34. Description:
  35. en: Views the ECS instances.
  36. zh-cn: 获取ECS实例。
  37. Action: 'ACS::SelectTargets'
  38. Properties:
  39. ResourceType: 'ALIYUN::ECS::Instance'
  40. Filters:
  41. - '{{ targets }}'
  42. Outputs:
  43. instanceIds:
  44. Type: List
  45. ValueSelector: 'Instances.Instance[].InstanceId'
  46. - Name: startInstance
  47. Action: 'ACS::ECS::StartInstance'
  48. Description:
  49. en: Starts the ECS instances.
  50. zh-cn: 启动实例。
  51. Properties:
  52. instanceId: '{{ ACS::TaskLoopItem }}'
  53. Loop:
  54. RateControl: '{{ rateControl }}'
  55. Items: '{{ getInstance.instanceIds }}'
  56. Outputs:
  57. instanceIds:
  58. Type: List
  59. Value: '{{ getInstance.instanceIds }}'
  • 批量启动ECS实例(JSON)
  1. {
  2. "FormatVersion": "OOS-2019-06-01",
  3. "Description": {
  4. "en": "Bulky starts the ECS instances.",
  5. "zh-cn": "批量启动ECS实例。",
  6. "name-en": "ACS-ECS-BulkyStartInstances",
  7. "name-zh-cn": "批量启动ECS实例",
  8. "categories": [
  9. "instance_manage"
  10. ]
  11. },
  12. "Parameters": {
  13. "targets": {
  14. "Type": "Json",
  15. "AssociationProperty": "Targets",
  16. "AssociationPropertyMetadata": {
  17. "ResourceType": "ALIYUN::ECS::Instance"
  18. }
  19. },
  20. "rateControl": {
  21. "Description": {
  22. "en": "Concurrency ratio of task execution.",
  23. "zh-cn": "任务执行的并发比率。"
  24. },
  25. "Type": "Json",
  26. "AssociationProperty": "RateControl",
  27. "Default": {
  28. "Mode": "Concurrency",
  29. "MaxErrors": 0,
  30. "Concurrency": 10
  31. }
  32. },
  33. "OOSAssumeRole": {
  34. "Description": {
  35. "en": "The RAM role to be assumed by OOS.",
  36. "zh-cn": "OOS扮演的RAM角色。"
  37. },
  38. "Type": "String",
  39. "Default": "OOSServiceRole"
  40. }
  41. },
  42. "RamRole": "{{ OOSAssumeRole }}",
  43. "Tasks": [
  44. {
  45. "Name": "getInstance",
  46. "Description": {
  47. "en": "Views the ECS instances.",
  48. "zh-cn": "获取ECS实例。"
  49. },
  50. "Action": "ACS::SelectTargets",
  51. "Properties": {
  52. "ResourceType": "ALIYUN::ECS::Instance",
  53. "Filters": [
  54. "{{ targets }}"
  55. ]
  56. },
  57. "Outputs": {
  58. "instanceIds": {
  59. "Type": "List",
  60. "ValueSelector": "Instances.Instance[].InstanceId"
  61. }
  62. }
  63. },
  64. {
  65. "Name": "startInstance",
  66. "Action": "ACS::ECS::StartInstance",
  67. "Description": {
  68. "en": "Starts the ECS instances.",
  69. "zh-cn": "启动实例。"
  70. },
  71. "Properties": {
  72. "instanceId": "{{ ACS::TaskLoopItem }}"
  73. },
  74. "Loop": {
  75. "RateControl": "{{ rateControl }}",
  76. "Items": "{{ getInstance.instanceIds }}"
  77. }
  78. }
  79. ],
  80. "Outputs": {
  81. "instanceIds": {
  82. "Type": "List",
  83. "Value": "{{ getInstance.instanceIds }}"
  84. }
  85. }
  86. }