全部产品

选择ACS::Choice

更新时间:2019-12-13 10:13:46

用途

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

语法

  • YAML格式
  1. Tasks:
  2. - Name: chooseNextTaskToRun # 任务名称
  3. Action: ACS::Choice
  4. Properties:
  5. DefaultTask: task1 #执行完该chooseNextTaskToRun任务,若无Choices中条件满足时,默认将执行名称为task1的任务。task1不允许是对参数引用获取的任务名,其必须是确定的字符串格式的任务名称。
  6. Choices:
  7. - When: condition1 # 待判断的条件1。
  8. NextTask: task2 # 当condition1为true时,执行完该chooseNextTaskToRun任务,将执行名称为task2的任务。task2不允许是对参数引用获取的任务名,其必须是确定的字符串格式的任务名称。
  9. - When: condition2 # 待判断的条件2。
  10. NextTask: task3 # 当condition2为true时,执行完该chooseNextTaskToRun任务,将执行名称为task3的任务。task3不允许是对参数引用获取的任务名,其必须是确定的字符串格式的任务名称。
  11. # 如有需要,Choices中还可有多个When判断条件。
  12. #...
  • JSON格式(请参照YAML注释说明)
  1. {
  2. "Tasks": [{
  3. "Name": "chooseNextTaskToRun",
  4. "Action": "ACS::Choice",
  5. "Properties": {
  6. "DefaultTask": "task1",
  7. "Choices": [
  8. {
  9. "When": "condition1",
  10. "NextTask": "task2"
  11. },
  12. {
  13. "When": "condition2",
  14. "NextTask": "task3"
  15. }]}}]
  16. }

示例

  • YAML格式

    1. FormatVersion: OOS-2019-06-01
    2. Description:
    3. en: Bulky restarts the ECS instances.
    4. zh-cn: 批量重启ECS实例。
    5. name-en: ACS-ECS-BulkyRebootInstances
    6. name-zh-cn: 批量重启ECS实例
    7. Parameters:
    8. targets:
    9. Type: Json
    10. AssociationProperty: Targets
    11. AssociationPropertyMetadata:
    12. ResourceType: 'ALIYUN::ECS::Instance'
    13. rateControl:
    14. Description:
    15. en: Concurrency ratio of task execution.
    16. zh-cn: 任务执行的并发比率。
    17. Type: Json
    18. AssociationProperty: RateControl
    19. OOSAssumeRole:
    20. Description:
    21. en: The RAM role to be assumed by OOS.
    22. zh-cn: OOS扮演的RAM角色。
    23. Type: String
    24. Default: OOSServiceRole
    25. RamRole: '{{ OOSAssumeRole }}'
    26. Tasks:
    27. - Name: getInstance
    28. Description:
    29. en: Views the ECS instances.
    30. zh-cn: 获取ECS实例。
    31. Action: 'ACS::SelectTargets'
    32. Properties:
    33. ResourceType: 'ALIYUN::ECS::Instance'
    34. Filters:
    35. - '{{ targets }}'
    36. Outputs:
    37. instanceIds:
    38. Type: List
    39. ValueSelector: 'Instances.Instance[].InstanceId'
    40. - Name: describeInstances
    41. Action: ACS::ExecuteAPI
    42. Description: Views A ECS instances Status.
    43. Properties:
    44. Service: ECS
    45. API: DescribeInstances
    46. Parameters:
    47. InstanceIds: '{{ getInstance.instanceIds }}'
    48. Outputs:
    49. status:
    50. Type: String
    51. ValueSelector: Instances.Instance[].Status
    52. - Name: whetherStatusRunning
    53. Action: 'ACS::Choice'
    54. Properties:
    55. DefaultTask: describeInstancesFinally
    56. Choices:
    57. - When:
    58. 'Fn::Equals':
    59. - Running
    60. - '{{ describeInstances.status }}'
    61. NextTask: rebootInstance
    62. - When:
    63. 'Fn::Equals':
    64. - Stopped
    65. - '{{ describeInstances.status }}'
    66. NextTask: startInstance
    67. - Name: startInstance
    68. Action: 'ACS::ECS::StartInstance'
    69. Description:
    70. en: Starts the ECS instances.
    71. zh-cn: 启动实例。
    72. Properties:
    73. instanceId: '{{ ACS::TaskLoopItem }}'
    74. Loop:
    75. RateControl: '{{ rateControl }}'
    76. Items: '{{ getInstance.instanceIds }}'
    77. - Name: rebootInstance
    78. Action: 'ACS::ECS::RebootInstance'
    79. Description:
    80. en: Restarts the ECS instances.
    81. zh-cn: 重启实例。
    82. Properties:
    83. instanceId: '{{ ACS::TaskLoopItem }}'
    84. Loop:
    85. RateControl: '{{ rateControl }}'
    86. Items: '{{ getInstance.instanceIds }}'
    87. - Name: describeInstancesFinally
    88. Action: ACS::ExecuteAPI
    89. Description: Views the ECS instances Status.
    90. Properties:
    91. Service: ECS
    92. API: DescribeInstances
    93. Parameters:
    94. InstanceIds: '{{ getInstance.instanceIds }}'
    95. Outputs:
    96. status:
    97. Type: String
    98. ValueSelector: Instances.Instance[].Status
    99. Outputs:
    100. instanceIds:
    101. Type: List
    102. Value: '{{ getInstance.instanceIds }}'
  • JSON格式
    1. {
    2. "FormatVersion": "OOS-2019-06-01",
    3. "Description": {
    4. "en": "Bulky restarts the ECS instances.",
    5. "zh-cn": "批量重启ECS实例。",
    6. "name-en": "ACS-ECS-BulkyRebootInstances",
    7. "name-zh-cn": "批量重启ECS实例"
    8. },
    9. "Parameters": {
    10. "targets": {
    11. "Type": "Json",
    12. "AssociationProperty": "Targets",
    13. "AssociationPropertyMetadata": {
    14. "ResourceType": "ALIYUN::ECS::Instance"
    15. }
    16. },
    17. "rateControl": {
    18. "Description": {
    19. "en": "Concurrency ratio of task execution.",
    20. "zh-cn": "任务执行的并发比率。"
    21. },
    22. "Type": "Json",
    23. "AssociationProperty": "RateControl"
    24. },
    25. "OOSAssumeRole": {
    26. "Description": {
    27. "en": "The RAM role to be assumed by OOS.",
    28. "zh-cn": "OOS扮演的RAM角色。"
    29. },
    30. "Type": "String",
    31. "Default": "OOSServiceRole"
    32. }
    33. },
    34. "RamRole": "{{ OOSAssumeRole }}",
    35. "Tasks": [
    36. {
    37. "Name": "getInstance",
    38. "Description": {
    39. "en": "Views the ECS instances.",
    40. "zh-cn": "获取ECS实例。"
    41. },
    42. "Action": "ACS::SelectTargets",
    43. "Properties": {
    44. "ResourceType": "ALIYUN::ECS::Instance",
    45. "Filters": [
    46. "{{ targets }}"
    47. ]
    48. },
    49. "Outputs": {
    50. "instanceIds": {
    51. "Type": "List",
    52. "ValueSelector": "Instances.Instance[].InstanceId"
    53. }
    54. }
    55. },
    56. {
    57. "Name": "describeInstances",
    58. "Action": "ACS::ExecuteAPI",
    59. "Description": "Views A ECS instances Status.",
    60. "Properties": {
    61. "Service": "ECS",
    62. "API": "DescribeInstances",
    63. "Parameters": {
    64. "InstanceIds": "{{ getInstance.instanceIds }}"
    65. }
    66. },
    67. "Outputs": {
    68. "status": {
    69. "Type": "String",
    70. "ValueSelector": "Instances.Instance[].Status"
    71. }
    72. }
    73. },
    74. {
    75. "Name": "whetherStatusRunning",
    76. "Action": "ACS::Choice",
    77. "Properties": {
    78. "DefaultTask": "describeInstancesFinally",
    79. "Choices": [
    80. {
    81. "When": {
    82. "Fn::Equals": [
    83. "Running",
    84. "{{ describeInstances.status }}"
    85. ]
    86. },
    87. "NextTask": "rebootInstance"
    88. },
    89. {
    90. "When": {
    91. "Fn::Equals": [
    92. "Stopped",
    93. "{{ describeInstances.status }}"
    94. ]
    95. },
    96. "NextTask": "startInstance"
    97. }
    98. ]
    99. }
    100. },
    101. {
    102. "Name": "startInstance",
    103. "Action": "ACS::ECS::StartInstance",
    104. "Description": {
    105. "en": "Starts the ECS instances.",
    106. "zh-cn": "启动实例。"
    107. },
    108. "Properties": {
    109. "instanceId": "{{ ACS::TaskLoopItem }}"
    110. },
    111. "Loop": {
    112. "RateControl": "{{ rateControl }}",
    113. "Items": "{{ getInstance.instanceIds }}"
    114. }
    115. },
    116. {
    117. "Name": "rebootInstance",
    118. "Action": "ACS::ECS::RebootInstance",
    119. "Description": {
    120. "en": "Restarts the ECS instances.",
    121. "zh-cn": "重启实例。"
    122. },
    123. "Properties": {
    124. "instanceId": "{{ ACS::TaskLoopItem }}"
    125. },
    126. "Loop": {
    127. "RateControl": "{{ rateControl }}",
    128. "Items": "{{ getInstance.instanceIds }}"
    129. }
    130. },
    131. {
    132. "Name": "describeInstancesFinally",
    133. "Action": "ACS::ExecuteAPI",
    134. "Description": "Views the ECS instances Status.",
    135. "Properties": {
    136. "Service": "ECS",
    137. "API": "DescribeInstances",
    138. "Parameters": {
    139. "InstanceIds": "{{ getInstance.instanceIds }}"
    140. }
    141. },
    142. "Outputs": {
    143. "status": {
    144. "Type": "String",
    145. "ValueSelector": "Instances.Instance[].Status"
    146. }
    147. }
    148. }
    149. ],
    150. "Outputs": {
    151. "instanceIds": {
    152. "Type": "List",
    153. "Value": "{{ getInstance.instanceIds }}"
    154. }}}