

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

# 使用 從 AWS 受管範本建立任務 AWS CLI
<a name="job-template-manage-cli-create"></a>

使用 AWS CLI 取得 AWS 受管範本的相關資訊，並使用這些範本建立任務。您便可以將任務另存為範本，然後建立自己的自訂範本。

## 列出受管範本
<a name="job-template-manage-cli-list"></a>

[https://docs.aws.amazon.com/cli/latest/reference/iot/list-managed-job-templates.html](https://docs.aws.amazon.com/cli/latest/reference/iot/list-managed-job-templates.html) AWS CLI 命令會列出 中的所有任務範本 AWS 帳戶。

```
 aws iot list-managed-job-templates
```

根據預設，執行此命令會顯示所有可用的 AWS 受管範本及其詳細資訊。

```
{
    "managedJobTemplates": [
        {
            "templateArn": "arn:aws:iot:region::jobtemplate/AWS-Reboot:1.0",
            "templateName": "AWS-Reboot",
            "description": "A managed job template for rebooting the device.",
            "environments": [
                "LINUX"
            ],
            "templateVersion": "1.0"
        },
        {
            "templateArn": "arn:aws:iot:region::jobtemplate/AWS-Remove-Application:1.0",
            "templateName": "AWS-Remove-Application",
            "description": "A managed job template for uninstalling one or more applications.",
            "environments": [
                "LINUX"
            ],
            "templateVersion": "1.0"
        },
        {
            "templateArn": "arn:aws:iot:region::jobtemplate/AWS-Stop-Application:1.0",
            "templateName": "AWS-Stop-Application",
            "description": "A managed job template for stopping one or more system services.",
            "environments": [
                "LINUX"
            ],
            "templateVersion": "1.0"
        },

        ...

        {
            "templateArn": "arn:aws:iot:us-east-1::jobtemplate/AWS-Restart-Application:1.0",
            "templateName": "AWS-Restart-Application",
            "description": "A managed job template for restarting one or more system services.",
            "environments": [
                "LINUX"
            ],
            "templateVersion": "1.0"
        }
    ]
}
```

如需詳細資訊，請參閱 [ListManagedJobTemplates](https://docs.aws.amazon.com/iot/latest/apireference/API_ListManagedJobTemplates.html)。

## 取得受管範本的詳細資訊
<a name="job-template-manage-cli-get"></a>

[https://docs.aws.amazon.com/cli/latest/reference/iot/describe-managed-job-template.html](https://docs.aws.amazon.com/cli/latest/reference/iot/describe-managed-job-template.html) AWS CLI 命令會取得指定任務範本的詳細資訊。指定任務範本名稱和選用範本版本。若未指定範本版本，則會傳回預先定義的預設版本。以下說明一個範例，執行命令以取得有關 `AWS-Download-File` 範本的詳細資訊。

```
aws iot describe-managed-job-template \
    --template-name AWS-Download-File
```

這個命令顯示範本詳細資訊和 ARN、其任務文件以及 `documentParameters` 參數，該參數為範本輸入參數的鍵值對清單。如需有關不同範本和輸入參數的詳細資訊，請參閱 [受管範本遠端動作和任務文件](job-templates-managed.md#job-template-manage-actions)。

**注意**  
只有在從 AWS 受管範本建立任務時，才能使用您使用此 API 時傳回的`documentParameters`物件。這個物件不能用於自訂任務範本。如需示範如何使用這個參數的範例，請參閱 [使用受管範本建立任務](#job-template-manage-cli-create-job)。

```
{
    "templateName": "AWS-Download-File",
    "templateArn": "arn:aws:iot:region::jobtemplate/AWS-Download-File:1.0",
    "description": "A managed job template for downloading a file.",
    "templateVersion": "1.0",
    "environments": [
        "LINUX"
    ],
    "documentParameters": [
        {
            "key": "downloadUrl",
            "description": "URL of file to download.",
            "regex": "(.*?)",
            "example": "http://www.example.com/index.html",
            "optional": false
        },
        {
            "key": "filePath",
            "description": "Path on the device where downloaded file is written.",
            "regex": "(.*?)",
            "example": "/path/to/file",
            "optional": false
        },
        {
            "key": "runAsUser",
            "description": "Execute handler as another user. If not specified, then handler is executed as the same user as device client.",
            "regex": "(.){0,256}",
            "example": "user1",
            "optional": true
        },
        {
            "key": "pathToHandler",
            "description": "Path to handler on the device. If not specified, then device client will use the current working directory.",
            "regex": "(.){0,4096}",
            "example": "/path/to/handler/script",
            "optional": true
        }
    ],
    "document": "{\"version\":\"1.0\",\"steps\":[{\"action\":{\"name\":\"Download-File\",\"type\":\"runHandler\",\"input\":{\"handler\":\"download-file.sh\",\"args\":[\"${aws:iot:parameter:downloadUrl}\",\"${aws:iot:parameter:filePath}\"],\"path\":\"${aws:iot:parameter:pathToHandler}\"},\"runAsUser\":\"${aws:iot:parameter:runAsUser}\"}}]}"
}
```

如需詳細資訊，請參閱 [DescribeManagedJobTemplate](https://docs.aws.amazon.com/iot/latest/apireference/API_DescribeManagedJobTemplate.html)。

## 使用受管範本建立任務
<a name="job-template-manage-cli-create-job"></a>

[https://docs.aws.amazon.com/cli/latest/reference/iot/create-job.html](https://docs.aws.amazon.com/cli/latest/reference/iot/create-job.html) AWS CLI 命令可用來從任務範本建立任務。這會將名為 `thingOne` 的裝置設為目標，並指定受管範本的 Amazon 資源名稱 (ARN)，以用作任務的基礎。您可以覆寫進階組態，例如逾時和取消組態，方法是傳遞 `create-job` 命令的相關聯參數。

這個範例說明如何使用 `AWS-Download-File` 範本建立任務。它也說明了如何使用 `document-parameters` 參數指定範本的輸入參數。

**注意**  
僅搭配 AWS 受管範本使用 `document-parameters` 物件。這個對象不能與自訂任務範本搭配使用。

```
aws iot create-job \
   --targets arn:aws:iot:region:account-id:thing/thingOne \ 
   --job-id "new-managed-template-job" \ 
   --job-template-arn arn:aws:iot:region::jobtemplate/AWS-Download-File:1.0 \
   --document-parameters downloadUrl=https://example.com/index.html,filePath=path/to/file
```

其中：
+ *region* 是 AWS 區域。
+ *account-id* 是唯一的 AWS 帳戶 數字。
+ `thingOne` 是任務所針對的 IoT 物件名稱。
+ *AWS-Download-File:1.0* 是受管範本的名稱。
+ `https://example.com/index.html` 是要下載檔案的 URL。
+ `https://pathto/file/index` 是裝置上用來存放下載檔案的路徑。

執行以下命令來建立範本 `AWS-Download-File` 的任務。

```
{
    "jobArn": "arn:aws:iot:region:account-id:job/new-managed-template-job",
    "jobId": "new-managed-template-job",
    "description": "A managed job template for downloading a file."
}
```

## 從受管範本建立自訂任務範本
<a name="job-template-manage-cli-create-jobtemplate"></a>

1. 使用受管範本建立任務，如前一節中所述。

1. 使用您建立的任務的 ARN 建立自訂任務範本。如需詳細資訊，請參閱 [從現有任務建立任務範本](job-templates-cli.md#job-templates-cli-create-from-job)。