All Products
Search
Document Center

ACS::Choice

Last Updated: Mar 04, 2020

Description

The ACS::Choice action can be used to determine the task to be run based on the judgment of the conditions For example, before you restart an instance, you want to query the status of the instance and select the task to be run based on the returned result. If the instance is in the Running state, the restart task is run. If the instance is in the Stopped state, the start task is run. If the instance is in other states, the query task is run.

Syntax

  • YAML format
  1. Tasks:
  2. - Name: chooseNextTaskToRun # The name of the task.
  3. Action: ACS::Choice
  4. Properties:
  5. DefaultTask: task1 # The default task that is run if no condition specified by Choices is met after the chooseNextTaskToRun task is run. The name of the task1 task cannot be the name of a parameter. It must be a certain string.
  6. Choices:
  7. - When: condition1 # The first condition for judgment.
  8. NextTask: task2 # The task that is run if the first condition is met.
  9. - When: condition2 # The second condition for judgment.
  10. NextTask: task3 # The task that is run if the second condition is met.
  11. # If necessary, you can use more WHEN conditions in the command.
  12. #...
  • JSON format (For more information, see the parameter description for the YAML format.)
  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. }

Examples

  • YAML format
  1. FormatVersion: OOS-2019-06-01
  2. Description:
  3. en: Bulky restarts the ECS instances.
  4. null-null-null
  5. name-en: ACS-ECS-BulkyRebootInstances
  6. null-null-null
  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. null-null-null
  17. Type: Json
  18. AssociationProperty: RateControl
  19. OOSAssumeRole:
  20. Description:
  21. en: The RAM role to be assumed by OOS.
  22. null-null-null
  23. Type: String
  24. Default: OOSServiceRole
  25. RamRole: '{{ OOSAssumeRole }}'
  26. Tasks:
  27. - Name: getInstance
  28. Description:
  29. en: Views the ECS instances.
  30. null-null-null
  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. null-null-null
  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. null-null-null
  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 format
  1. {
  2. "FormatVersion": "OOS-2019-06-01",
  3. "Description": {
  4. "en": "Bulky restarts the ECS instances.",
  5. null-null-null
  6. "name-en": "ACS-ECS-BulkyRebootInstances",
  7. null-null-null
  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. null-null-null
  21. },
  22. "Type": "Json",
  23. "AssociationProperty": "RateControl"
  24. },
  25. "OOSAssumeRole": {
  26. "Description": {
  27. "en": "The RAM role to be assumed by OOS.",
  28. null-null-null
  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. null-null-null
  41. },
  42. "Action": "ACS::SelectTargets",
  43. "Properties": {
  44. "ResourceType": "ALIYUN::ECS::Instance",
  45. "Filters": ["{{ targets }}"]
  46. },
  47. "Outputs": {
  48. "instanceIds": {
  49. "Type": "List",
  50. "ValueSelector": "Instances.Instance[].InstanceId"
  51. }
  52. }
  53. },
  54. {
  55. "Name": "describeInstances",
  56. "Action": "ACS::ExecuteAPI",
  57. "Description": "Views A ECS instances Status.",
  58. "Properties": {
  59. "Service": "ECS",
  60. "API": "DescribeInstances",
  61. "Parameters": {
  62. "InstanceIds": "{{ getInstance.instanceIds }}"
  63. }
  64. },
  65. "Outputs": {
  66. "status": {
  67. "Type": "String",
  68. "ValueSelector": "Instances.Instance[].Status"
  69. }
  70. }
  71. },
  72. {
  73. "Name": "whetherStatusRunning",
  74. "Action": "ACS::Choice",
  75. "Properties": {
  76. "DefaultTask": "describeInstancesFinally",
  77. "Choices": [
  78. {
  79. "When": {
  80. "Fn::Equals": ["Running",
  81. "{{ describeInstances.status }}"]
  82. },
  83. "NextTask": "rebootInstance"
  84. },
  85. {
  86. "When": {
  87. "Fn::Equals": ["Stopped",
  88. "{{ describeInstances.status }}"]
  89. },
  90. "NextTask": "startInstance"
  91. }
  92. ]
  93. }
  94. },
  95. {
  96. "Name": "startInstance",
  97. "Action": "ACS::ECS::StartInstance",
  98. "Description": {
  99. "en": "Starts the ECS instances.",
  100. null-null-null
  101. },
  102. "Properties": {
  103. "instanceId": "{{ ACS::TaskLoopItem }}"
  104. },
  105. "Loop": {
  106. "RateControl": "{{ rateControl }}",
  107. "Items": "{{ getInstance.instanceIds }}"
  108. }
  109. },
  110. {
  111. "Name": "rebootInstance",
  112. "Action": "ACS::ECS::RebootInstance",
  113. "Description": {
  114. "en": "Restarts the ECS instances.",
  115. null-null-null
  116. },
  117. "Properties": {
  118. "instanceId": "{{ ACS::TaskLoopItem }}"
  119. },
  120. "Loop": {
  121. "RateControl": "{{ rateControl }}",
  122. "Items": "{{ getInstance.instanceIds }}"
  123. }
  124. },
  125. {
  126. "Name": "describeInstancesFinally",
  127. "Action": "ACS::ExecuteAPI",
  128. "Description": "Views the ECS instances Status.",
  129. "Properties": {
  130. "Service": "ECS",
  131. "API": "DescribeInstances",
  132. "Parameters": {
  133. "InstanceIds": "{{ getInstance.instanceIds }}"
  134. }
  135. },
  136. "Outputs": {
  137. "status": {
  138. "Type": "String",
  139. "ValueSelector": "Instances.Instance[].Status"
  140. }
  141. }
  142. }
  143. ],
  144. "Outputs": {
  145. "instanceIds": {
  146. "Type": "List",
  147. "Value": "{{ getInstance.instanceIds }}"
  148. }}}