

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 自動化您的管理任務 AWS OpsHub
<a name="automate-task"></a>

您可以使用 AWS OpsHub 來自動化在 Snowball Edge 上經常執行的操作任務。您可以建立任務來重複執行您可能想要在資源上執行的動作，例如重新啟動虛擬伺服器、停止與 Amazon EC2-compatible執行個體等。您提供的自動化文件可安全地執行操作任務，並在 AWS 資源上大量執行操作。您也可以排定一般 IT 工作流程。

**注意**  
叢集不支援自動執行任務。  
若要使用任務，必須先啟動 Amazon EC2 Systems Manager 服務。如需詳細資訊，請參閱[在 Snowball Edge 上啟用 Snowball Edge 裝置管理](https://docs.aws.amazon.com/snowball/latest/developer-guide/aws-sdm.html#enable-sdm)。

**Topics**
+ [使用 建立和啟動任務 AWS OpsHub](#create-task)
+ [在 中檢視任務的詳細資訊 AWS OpsHub](#view-task)
+ [在 中刪除任務 AWS OpsHub](#delete-task)

## 使用 建立和啟動任務 AWS OpsHub
<a name="create-task"></a>

當您建立任務時，您可以指定任務應執行的資源類型，然後提供任務文件，當中包含執行任務的指示。任務文件是 YAML 或 JSON 格式。然後，您可以為任務提供必要的參數，並開始任務。

**建立任務**

1. 在儀表板的 **Launch tasks (啟動任務)** 區段中，選擇 **Get started (開始使用)** 以開啟 **Tasks (任務)** 頁面。如果您已建立任務，它們會出現在 **Tasks (任務)** 下方。

1. 選擇 **Create task (建立任務)** 並提供任務的詳細資訊。

1. 對於 **Name (名稱)**，輸入任務的唯一名稱。
**提示**  
名稱長度必須介於 3 到 128 個字元之間。有效字元為 `a-z`、`A-Z`、`0-9`、`.`、`_` 和 `-`。

1. 您也可以選擇從 **Target type-optional (目標類型-選用)** 清單中選擇目標類型。這是您想要執行任務的資源類型。

   例如，您可以**/AWS::EC2::Instance**指定 讓任務在 Amazon EC2-compatible執行個體上執行**/**，或在所有資源類型上執行。

1. 在**內容**區段中，選擇 **YAML** 或 **JSON**，並提供執行任務的指令碼。您有兩個選項，YAML 或 JSON 格式。如需範例，請參閱 [中的任務範例 AWS OpsHub](#task-examples)。

1. 選擇**建立**。接著您建立的任務會出現在 **Tasks (任務)** 頁面上。

**開始任務**

1. 在儀表板的 **Launch tasks (啟動任務)** 區段中，選擇 **Get started (開始使用)** 以開啟 **Tasks (任務)** 頁面。您的任務會出現在 **Tasks (任務)** 下方。

1. 選擇您的任務以開啟 **Start task (開始任務)** 頁面。

1. 選擇 **Simple execution (簡單執行)** 在目標上執行。

   選擇 **Rate control (速率控制)** 以在多個目標上安全執行，並定義並行和錯誤臨界值。對於此選項，您可以在 **Rate control (速率控制)** 區段中提供額外的目標和錯誤臨界值資訊。

1. 提供必要的輸入參數，然後選擇 **Start taks (開始任務)**。

   任務的狀態為 **Pending (待定)**，而當任務已順利執行時，會變更為 **Success (成功)**。

### 中的任務範例 AWS OpsHub
<a name="task-examples"></a>

下列範例會重新啟動與 Amazon EC2-compatible執行個體。它需要兩個輸入參數：`endpoint` 和 `instance ID`。

*YAML 範例*

```
description: Restart EC2 instance
schemaVersion: '0.3'
parameters:
  Endpoint:
    type: String
    description: (Required) EC2 Service Endpoint URL
  Id:
    type: String
    description: (Required) Instance Id
mainSteps:
  - name: restartInstance
    action: aws:executeScript
    description: Restart EC2 instance step
    inputs:
      Runtime: python3.7
      Handler: restart_instance
      InputPayload:
        Endpoint: "{{ Endpoint }}"
        Id: "{{ Id }}"
      TimeoutSeconds: 30
      Script: |-
        import boto3
        import time
        def restart_instance(payload, context):
            ec2_endpoint = payload['Endpoint']
            instance_id = payload['Id']
            ec2 = boto3.resource('ec2', endpoint_url=ec2_endpoint)
            instance = ec2.Instance(instance_id)
            if instance.state['Name'] != 'stopped':
                instance.stop()
                instance.wait_until_stopped()
            instance.start()
            instance.wait_until_running()
            return {'InstanceState': instance.state}
```

*JSON 範例*

```
{
  "description" : "Restart EC2 instance",
  "schemaVersion" : "0.3",
  "parameters" : {
    "Endpoint" : {
      "type" : "String",
      "description" : "(Required) EC2 Service Endpoint URL"
    },
    "Id" : {
      "type" : "String",
      "description" : "(Required) Instance Id"
    }
  },
  "mainSteps" : [ {
    "name" : "restartInstance",
    "action" : "aws:executeScript",
    "description" : "Restart EC2 instance step",
    "inputs" : {
      "Runtime" : "python3.7",
      "Handler" : "restart_instance",
      "InputPayload" : {
        "Endpoint" : "{{ Endpoint }}",
        "Id" : "{{ Id }}"
      },
      "TimeoutSeconds" : 30,
      "Script" : "import boto3\nimport time\ndef restart_instance(payload, context):\n    
            ec2_endpoint = payload['Endpoint']\n    instance_id = payload['Id']\n    
            ec2 = boto3.resource('ec2', endpoint_url=ec2_endpoint)\n    
            instance = ec2.Instance(instance_id)\n    
            if instance.state['Name'] != 'stopped':\n        
            instance.stop()\n        
            instance.wait_until_stopped()\n    
            instance.start()\n    
            instance.wait_until_running()\n    
            return {'InstanceState': instance.state}"
    }
  } ]
}
```

## 在 中檢視任務的詳細資訊 AWS OpsHub
<a name="view-task"></a>

您可以檢視管理任務的詳細資訊，例如執行任務所需的說明和參數。

**若要檢視任務的詳細資訊**

1. 在儀表板的 **Launch tasks (啟動任務)** 區段中，選擇 **Get started (開始使用)** 以開啟 **Tasks (任務)** 頁面。

1. 在 **Tasks (任務)** 頁面上，找出並選擇您要查看詳細資訊的任務。

1. 選擇 **View details (檢視詳細資訊)**，然後選擇其中一個索引標籤以查看詳細資訊。例如，**Parameters (參數)** 索引標籤會顯示指令碼中的輸入參數。

## 在 中刪除任務 AWS OpsHub
<a name="delete-task"></a>

請依照下列步驟刪除管理任務。

**刪除任務**

1. 在儀表板的 **Launch tasks (啟動任務)** 區段中，選擇 **Get started (開始使用)** 以開啟 **Tasks (任務)** 頁面。

1. 找出您要刪除的任務。選擇任務，然後選擇 **Delete (刪除)**。