All Products
Search
Document Center

Run commands on multiple ECS instances

Last Updated: Aug 06, 2020

Background

It is common that Alibaba Cloud users remotely access and perform operations on Elastic Compute Service (ECS) instances. For example, they may remotely install or uninstall software, or start or stop a process on the ECS instances. In most cases, they need to run the same command on multiple ECS instances and then aggregate the execution result.
Users can remotely connect to ECS instances through Secure Shell (SSH). However, it is not secure to enable the SSH port of an ECS instance for Internet access. To resolve the security issues, users can create jump servers and access ECS instances through the jump servers. However, this method increases complexity and costs.
Ansible, a popular open-source operation and maintenance (O&M) tool in the industry, can also be used for remote access. However, its authentication is independent of the Alibaba Cloud account system. That is, permissions cannot be controlled through Alibaba Cloud tools. In addition, its operations logs cannot be audited by Alibaba Cloud.
If users are using ECS instances that run the Windows operating system, they can remotely run commands by using PowerShell. However, the configuration and maintenance process may be difficult.
To resolve the preceding issues, we recommend that you use Operation Orchestration Service (OOS) and Cloud Assistant provided by Alibaba Cloud. You can run commands on multiple ECS instances at a time with these two services. Their features make it easy, secure, and convenient to perform O&M operations on ECS instances. Cloud Assistant provides various atom actions for you to take on ECS instances. You can use OOS to manage scheduled and event-driven O&M tasks. It also allows you to handle multiple O&M tasks at a time and create custom templates for O&M operations.

Preparations

The following two steps are required:
First, log on to the OOS console and click Enable Now to activate OOS. This service is free of charge. To activate OOS, visit https://oos.console.aliyun.com/.


Then, install and configure the Alibaba Cloud command line interface (CLI) V3.0.19 or later. To download the installation package of Alibaba Cloud CLI from Github, visit https://github.com/aliyun/aliyun-cli/releases. For more information about the configuration, see https://www.alibabacloud.com/help/doc-detail/121258.html.

Use a public template

Run the following command in a local shell to remotely run the shell command “echo 123” on two Linux-based ECS instances named [“i-id45678zxcvb” and “i-id45679zxcvb”] in the China (Beijing) region:

  1. 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 public template ACS-ECS-BulkyRunCommand with the ResourceIds and commandContent parameters specified.
If “ExecutionId”: “exec-xxxxx” appears in the JSON-formatted return result, the execution is in progress.
Record the value of the ExecutionId parameter and use it in the following ListExecutions command to query the execution progress and result:

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

If the execution is in progress, the following result is returned, where the value of the Status parameter is Running. The value of the TotalTasks parameter indicates the total number of commands, and that of the SuccessTasks parameter indicates the number of commands that have been successfully run. The difference between the two values indicates the number of commands pending to be run.

  1. {
  2. "Execution": {
  3. "Outputs": {},
  4. "TemplateName": "ACS-ECS-BulkyRunCommand",
  5. "Parameters": {
  6. "commandType": "RunShellScript",
  7. "OOSAssumeRole": "",
  8. "rateControl": {
  9. "maxErrors": 0,
  10. "mode": "Concurrency"
  11. },
  12. "targets": {
  13. "ResourceIds": [
  14. "i-id45678zxcvb","i-id45679zxcvb"
  15. ],
  16. "Type": "ResourceIds"
  17. },
  18. "commandContent": "echo 123"
  19. },
  20. "Counters": {
  21. "Failed": 0,
  22. "Success": 0,
  23. "Total": 0
  24. },
  25. "ExecutedBy": "aliyun-account1",
  26. "LoopMode": "Automatic",
  27. "Mode": "Automatic",
  28. "TemplateId": "t-123456zxcvb",
  29. "Status": "Running",
  30. "TemplateVersion": "v2",
  31. "SafetyCheck": "Skip",
  32. "StartDate": "2019-10-15T07:22:03Z",
  33. "ExecutionId": "exec-id123456zxcvb",
  34. "CurrentTasks": []
  35. },
  36. "RequestId": "1A9B1817-0530-470C-8640-BADADADB220BD"
  37. }

You can run the ListExecutions command multiple times until the final result appears in the value of the Outputs parameter. The following figure shows the final result of this example.
OOS

Use a custom template

You can also create and use custom templates with fixed parameter settings to simplify the command.
The following code shows an example of the code in a custom template. You can customize the template based on your needs.

  1. aliyun oos CreateTemplate --region cn-hangzhou --TemplateName sample123 --Content '{
  2. "FormatVersion": "OOS-2019-06-01",
  3. "Tasks": [
  4. {
  5. "Name": "runCommand",
  6. "Action": "ACS::ECS::RunCommand",
  7. "Properties": {
  8. "commandContent": "echo 1234",
  9. "instanceId": "{{ ACS::TaskLoopItem }}",
  10. "commandType": "RunShellScript"
  11. },
  12. "Loop": {
  13. "Items": ["i-id45678zxcvb","i-id45679zxcvb"],
  14. "Outputs": {
  15. "commandOutputs": {
  16. "AggregateType": "Fn::ListJoin",
  17. "AggregateField": "commandOutput"
  18. }
  19. }
  20. },
  21. "Outputs": {
  22. "commandOutput": {
  23. "Type": "String",
  24. "ValueSelector": "invocationOutput"
  25. }
  26. }
  27. }
  28. ],
  29. "Outputs": {
  30. "commandOutputs": {
  31. "Type": "List",
  32. "Value": "{{ runCommand.commandOutputs }}"
  33. }
  34. }
  35. }'

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

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

In this example, the StartExecution operation is called to execute the custom template named sample123. No other parameter is required to be set.
The preceding operations are performed by running commands on CLI. You can also perform these operations in the OOS console. To visit the OOS console, click https://oos.console.aliyun.com/.

Learn more

OOS is an automatic O&M platform provided by Alibaba Cloud. You can use OOS to manage event-driven, scheduled, and cross-region O&M tasks, or to easily handle multiple O&M tasks at a time. It also allows you to run commands on ECS instances, create, release, enable, and disable ECS instances, modify instance configurations, upgrade the network bandwidth, and attach the disk to an ECS instance.
For more information, join the OOS customer support group (DingTalk group ID: 23330931). We will offer help as soon as possible.
To log on to the OOS console, visit https://oos.console.aliyun.com.