All Products
Search
Document Center

CloudOps Orchestration Service:Run a command on multiple ECS instances at a time

Last Updated:Aug 21, 2026

This topic describes how to run a command on multiple Elastic Compute Service (ECS) instances at a time.

Background information

Alibaba Cloud allows you to run commands on a remote ECS instance. This is one of the most common O&M operations of Alibaba Cloud. For example, you can run a command to install or uninstall software or start or stop a process on a specific ECS instance. In most cases, you may also need to run the same command on multiple ECS instances and aggregate the results.

For example, you can run a related command on multiple business applications at a time to update runtime environments by performing rolling updates or run code. This improves the reliability of application deployment.

A practical solution is to connect to the ECS instances over Secure Shell (SSH). However, this may involve potential risks if you access the SSH port of an ECS instance over the Internet. To handle the security issue, you can create jump servers and use them to access ECS instances. However, this may increase the operation complexity and costs.

Ansible is a popular open source O&M tool in the industry, while the authentication method of Ansible is independent of the Alibaba Cloud account system. If you use Ansible for remote access, you cannot use Alibaba Cloud tools to control the access or audit the operational records of Ansible by using Alibaba Cloud.

If you use Windows ECS instances, you can use PowerShell to run commands. However, this creates significant challenges when you configure and maintain the ECS instances.

To handle the preceding issues, we recommend that you integrate Alibaba Cloud Operation Orchestration Service (OOS) with Cloud Assistant. This allows you to run commands on multiple ECS instances at a time. Cloud Assistant provides a number of atomic features that are used to run commands on ECS instances. OOS allows you to manage scheduled and event-driven O&M tasks, process multiple O&M tasks at a time, and create custom templates for O&M tasks. This way, the integration of Cloud Assistant and OOS provides a secure, easy, and efficient method to perform O&M tasks on ECS instances.

Preparations

Make sure that Alibaba Cloud Command Line Interface (CLI) V3.0.19 or later is installed and configured. To download Alibaba Cloud CLI, visit https://github.com/aliyun/aliyun-cli/releases at GitHub. For more information about the configuration, see Interactive configuration (fast).

Execute a public template

The following command provides an example on how to run the shell command echo 123 on the i-id45678zxcvb and i-id45679zxcvb Linux ECS instances in the China (Beijing) region by using the local shell terminal:

aliyun oos StartExecution --region cn-hangzhou --TemplateName ACS-ECS-BulkyRunCommand --Parameters '{"commandType":"RunShellScript", "commandContent":"echo 123",  "targets":{"Type":"ResourceIds", "ResourceIds":["i-id45678zxcvb","i-id45679zxcvb"]}, "rateControl":{"maxErrors":0,"mode":"Concurrency"}, "OOSAssumeRole":"" }'

In the preceding command, the StartExecution operation is called to execute the ACS-ECS-BulkyRunCommand public template that includes the ResourceIds and commandContent parameters. If "ExecutionId":"exec-xxxxx" is returned in the JSON-formatted result, the command is in progress. Record the value of the ExecutionId parameter and specify the value in the following ListExecutions command to query the progress and result:

aliyun oos ListExecutions --region cn-hangzhou --ExecutionId "exec-id123456zxcvb"

If the command is in progress, a value of Running is returned for the Status parameter in the result. The value of the TotalTasks parameter indicates the total number of commands. The value of the SuccessTasks parameter indicates the number of successful commands. The difference between the two values indicates the number of pending commands.

{
        "Execution": {
                "Outputs": {},
                "TemplateName": "ACS-ECS-BulkyRunCommand",
                "Parameters": {
                        "commandType": "RunShellScript",
                        "OOSAssumeRole": "",
                        "rateControl": {
                                "maxErrors": 0,
                                "mode": "Concurrency"
                        },
                        "targets": {
                                "ResourceIds": [
                                        "i-id45678zxcvb","i-id45679zxcvb"
                                ],
                                "Type": "ResourceIds"
                        },
                        "commandContent": "echo 123"
                },
                "Counters": {
                        "Failed": 0,
                        "Success": 0,
                        "Total": 0
                },
                "ExecutedBy": "aliyun-account1",
                "LoopMode": "Automatic",
                "Mode": "Automatic",
                "TemplateId": "t-123456zxcvb",
                "Status": "Running",
                "TemplateVersion": "v2",
                "SafetyCheck": "Skip",
                "StartDate": "2019-10-15T07:22:03Z",
                "ExecutionId": "exec-id123456zxcvb",
                "CurrentTasks": []
        },
        "RequestId": "1A9B1817-0530-470C-8640-BADADADB220BD"
}

You can repeatedly run the ListExecutions command until the final result is returned in the Outputs parameter. The following figure shows the final result of this example.

Create a custom template

You can create and run custom templates in which the required parameters are fixed. This simplifies the process to run commands. The following command provides an example on how to create a custom template. You can customize the template based on your business requirements.

aliyun oos CreateTemplate --region cn-hangzhou --TemplateName sample123 --Content '{
"FormatVersion": "OOS-2019-06-01",
"Tasks": [
{
"Name": "runCommand",
"Action": "ACS::ECS::RunCommand",
"Properties": {
"commandContent": "echo 1234",
"instanceId": "{{ ACS::TaskLoopItem }}",
"commandType": "RunShellScript"
},
"Loop": {
"Items": ["i-id45678zxcvb","i-id45679zxcvb"],
"Outputs": {
"commandOutputs": {
"AggregateType": "Fn::ListJoin",
"AggregateField": "commandOutput"
}
}
},
"Outputs": {
"commandOutput": {
"Type": "String",
"ValueSelector": "invocationOutput"
}
}
}
],
"Outputs": {
"commandOutputs": {
"Type": "List",
"Value": "{{ runCommand.commandOutputs }}"
}
}
}'                

After the custom template is created, run the following command to execute the template:

aliyun oos StartExecution --region cn-hangzhou --TemplateName sample123 --Parameters '{}'                

In this example, the StartExecution operation is called to execute the sample123 custom template. You do not need to specify extra parameters. The preceding operations are performed by running commands in CLI. You can also perform these operations in the OOS console.

Additional information

Alibaba Cloud OOS is an automatic O&M platform that allows you to manage scheduled, event-driven, and cross-region O&M tasks and process multiple O&M tasks at a time. Besides running commands, you can also use OOS to perform other operations on ECS instances. For example, you can create, release, start, and stop ECS instances, modify configurations and upgrade the network bandwidth of ECS instances, and attach disks to ECS instances. For more information about OOS, join the DingTalk group (Number: 23330931) for online support.

To log on to the OOS console, visit https://oos.console.alibabacloud.com.