All Products
Search
Document Center

CloudOps Orchestration Service:ACS-ECS-ScheduleToRunCommand

Last Updated:Jan 06, 2025

Template name

ACS-ECS-ScheduleToRunCommand

Execute Now

Template description

Runs a Cloud Assistant command on multiple Elastic Compute Service (ECS) instances as scheduled. This applies to scenarios in which you want to run commands on ECS instances such as system O&M and automation. When you configure this template, you must specify the following parameters: regionId: the region of the ECS instances, triggerCron: the rules that trigger the task, targets: the ECS instances, commandType and commandContent. After the template is executed, the result is returned so that you can check the execution status.

Template type

Automated

Owner

Alibaba Cloud

Input parameters

Parameter

Description

Type

Required

Default value

Limit

triggerEndDate

The end time of the scheduled task.

String

Yes

targets

The ECS instances on which you want to run a Cloud Assistant command.

Json

Yes

regionId

The region ID.

String

No

{{ ACS::RegionId }}

triggerCron

The CRON expression.

String

No

0 0 12 ? * *

timeZone

The time zone.

String

No

UTC

commandType

The type of the Cloud Assistant command.

String

No

RunShellScript

commandContent

The Cloud Assistant command.

String

No

echo hello

The command can be up to 16,384 bytes in length.

workingDir

The working directory of the command in the ECS instance.

String

No

""

timeout

The timeout period.

Number

No

600

rateControl

The rate control settings.

Json

No

{'Mode': 'Concurrency', 'MaxErrors': 0, 'Concurrency': 10}

OOSAssumeRole

The Resource Access Management (RAM) role that is assumed by CloudOps Orchestration Service (OOS).

String

No

OOSServiceRole

Output parameters

Parameter

Description

Type

commandOutputs

List

Permission policy that is required to execute the template

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:DescribeInstances",
                "ecs:DescribeInvocationResults",
                "ecs:DescribeInvocations",
                "ecs:RunCommand"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

References

For more information, see ACS-ECS-ScheduleToRunCommand.yml at GitHub.

Template content

FormatVersion: OOS-2019-06-01
Description:
  en: 'Schedule to run command on ECS instances, applicable to scenarios that require periodically running commands on multiple ECS instances, such as system maintenance and task automation. During configuration, users need to provide the following required parameter information: Region ID (regionId), which specifies the region of the ECS instances where the commands will be run, Cron Expression (triggerCron), which defines the schedule for the task, Target Instances (targets), which defines the specific ECS instances to be operated on, and Command Type (commandType) and Command Content (commandContent). Upon execution, the template will return a list of the output results of the run command, enabling users to verify the status of the command executions.'
  zh-cn: the description in Chinese
  name-en: ACS-ECS-ScheduleToRunCommand
  name-zh-cn: the description in Chinese
  categories:
    - time_trigger
Parameters:
  regionId:
    Type: String
    Label:
      en: RegionId
      zh-cn: the description in Chinese
    AssociationProperty: RegionId
    Default: '{{ ACS::RegionId }}'
  triggerCron:
    Description:
      en: 'refer them here: https://help.aliyun.com/document_detail/169784.html'
      zh-cn: the description in Chinese
    Label:
      en: Cronexpression
      zh-cn: the description in Chinese
    Type: String
    AssociationProperty: Cron
    Default: 0 0 12 ?  * *
  triggerEndDate:
    Description:
      en: 'Format: yyyy-MM-ddTHH:mm:ssZ.'
      zh-cn: the description in Chinese
    Label:
      en: triggerEndDate
      zh-cn: the description in Chinese
    Type: String
    AssociationProperty: DateTime
    AssociationPropertyMetadata:
      Format: 'YYYY-MM-DDTHH:mm:ssZ'
  timeZone:
    Label:
      en: timeZone
      zh-cn: the description in Chinese
    Type: String
    AssociationProperty: TimeZone
    Default: UTC
  targets:
    Type: Json
    Label:
      en: TargetInstance
      zh-cn: the description in Chinese
    AssociationProperty: Targets
    AssociationPropertyMetadata:
      ResourceType: 'ALIYUN::ECS::Instance'
      RegionId: regionId
      Status: Running
  commandType:
    Label:
      en: CommandType
      zh-cn: the description in Chinese
    Type: String
    AllowedValues:
      - RunBatScript
      - RunPowerShellScript
      - RunShellScript
    Default: RunShellScript
  commandContent:
    Label:
      en: CommandContent
      zh-cn: the description in Chinese
    Type: String
    MaxLength: 16384
    AssociationProperty: Code
    Default: echo hello
  workingDir:
    Description:
      en: '<td class="entry colsep-1 rowsep-1">
             
            <ul class="ul">
            <li class="li"> Linux instances: the home directory of the administrator (the root user), which is<code class="ph codeph">/root</code>.</li>
            <li class="li"> Windows instances: the directory where the Cloud Assistant client process resides,such as <code class="ph codeph">C:\Windows\System32</code>.</li>
            </ul>
          </td>'
      zh-cn: <td class="entry colsep-1 rowsep-1">
               
              <ul class="ul">
                 </li>
                 </li>
              </ul>
            </td>
    Label:
      en: WorkingDir
      zh-cn: the description in Chinese
    Type: String
    Default: ''
  timeout:
    Label:
      en: Timeout
      zh-cn: the description in Chinese
    Type: Number
    Default: 600
  rateControl:
    Label:
      en: RateControl
      zh-cn: the description in Chinese
    Type: Json
    AssociationProperty: RateControl
    Default:
      Mode: Concurrency
      MaxErrors: 0
      Concurrency: 10
  OOSAssumeRole:
    Label:
      en: OOSAssumeRole
      zh-cn: the description in Chinese
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: timerTrigger
    Action: ACS::TimerTrigger
    Description:
      en: Triggers a task as scheduled by specifying type and expression
      zh-cn: the description in Chinese
    Properties:
      Type: cron
      Expression: '{{ triggerCron }}'
      EndDate: '{{ triggerEndDate }}'
      TimeZone: '{{ timeZone }}'
  - Name: getInstance
    Description:
      en: Views the ECS instances
      zh-cn: the description in Chinese
    Action: 'ACS::SelectTargets'
    Properties:
      ResourceType: 'ALIYUN::ECS::Instance'
      RegionId: '{{ regionId }}'
      Filters:
        - '{{ targets }}'
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: 'Instances.Instance[].InstanceId'
  - Name: runCommand
    Action: 'ACS::ECS::RunCommand'
    Description:
      en: Execute cloud assistant command
      zh-cn: the description in Chinese
    Properties:
      regionId: '{{ regionId }}'
      commandContent: '{{ commandContent }}'
      instanceId: '{{ ACS::TaskLoopItem }}'
      commandType: '{{ commandType }}'
      workingDir: '{{ workingDir }}'
      timeout: '{{ timeout }}'
    Loop:
      RateControl: '{{ rateControl }}'
      Items: '{{ getInstance.instanceIds }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
Outputs:
  commandOutputs:
    Type: List
    Value: '{{ runCommand.commandOutputs }}'
Metadata:
  ALIYUN::OOS::Interface:
    ParameterGroups:
      - Parameters:
          - timeZone
          - triggerCron
          - triggerEndDate
        Label:
          default:
            zh-cn: the description in Chinese
            en: Timer Trigger Configure
      - Parameters:
          - commandType
          - commandContent
          - workingDir
          - timeout
        Label:
          default:
            zh-cn: the description in Chinese
            en: run command options
      - Parameters:
          - regionId
          - targets
        Label:
          default:
            zh-cn: the description in Chinese
            en: Select Ecs Instances
      - Parameters:
          - rateControl
          - OOSAssumeRole
        Label:
          default:
            zh-cn: the description in Chinese
            en: Control Options