使用文件 - AWS Systems Manager

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

使用文件

本節包含有關如何使用及處理 SSM 文件的資訊。

在 State Manager 關聯中使用 SSM 文件

如果您為 State Manager 建立一個 SSM 文件 (AWS Systems Manager 的一個功能),在將文件新增至系統之後,您必須將文件與受管執行個體建立關聯。如需更多詳細資訊,請參閱 在 Systems Manager 中使用關聯

在 State Manager 關聯中使用 SSM 文件時,請記住下列詳細資訊。

  • 透過使用不同的文件,建立不同的 State Manager 關聯,您可以指派多個文件到一個目標。

  • 如果您使用有衝突的外掛程式來建立文件 (例如,網域加入和網域移除),最終狀態會是外掛程式最後一次執行的結果。State Manager 不會驗證文件中的邏輯序列或指令的合理性或外掛程式。

  • 當處理文件時、執行個體的關聯會先被套用,接下來標籤族群的關聯才會被套用。如果執行個體屬於多個標籤群組,那些各標籤族群中的文件將不會照任何特定順序執行。如果執行個體透過自己的 ID 直接鎖定多的文件、那些文件不會照特定順序執行。

  • 如果您為 State Manager 變更 SSM 政策文件的預設版本,任何關聯在下一次使用該文件時將會使用新的預設版本,Systems Manager 會將此關聯套用到執行個體。

  • 如果您使用與您共用的 SSM 文件來建立關聯,然後擁有者停止與您共用文件,則您的關聯不再具備該文件的存取權。不過,如果擁有者稍後再次與您共用相同 SSM 的文件,您的關聯會自動進行重新對應。

比較 SSM 文件版本

您可以在 Systems Manager 文件主控台中比較 AWS Systems Manager (SSM) 文件版本之間的內容差異。比較 SSM 文件的版本時,會反白顯示版本內容之間的差異。

若要比較 SSM 文件內容 (主控台)
  1. 開啟位於 https://console.aws.amazon.com/systems-manager/ 的 AWS Systems Manager 主控台。

  2. 在導覽窗格中,選擇 Documents (文件)

    -或-

    如果 AWS Systems Manager 首頁先開啟,選擇選單圖示 ( 
    The menu icon
  ) 以開啟導覽窗格,然後在導覽窗格中,選擇 Documents (文件)。

  3. 在文件清單中,選擇您要比較其內容的文件。

  4. Content (內容) 索引標籤中,選取 Compare versions (比較版本),然後選擇您要與之比較內容的文件版本。

建立 SSM 文件 (主控台)

撰寫 SSM 文件內容 中所述,建立自訂 SSM 文件的內容後,您可以使用 Systems Manager 主控台,以您的內容建立 SSM 文件。

若要建立 SSM 文件 (主控台)
  1. 開啟位於 https://console.aws.amazon.com/systems-manager/ 的 AWS Systems Manager 主控台。

  2. 在導覽窗格中,選擇 Documents (文件)

    -或-

    如果 AWS Systems Manager 首頁先開啟,選擇選單圖示 ( 
    The menu icon
  ) 以開啟導覽窗格,然後在導覽窗格中,選擇 Documents (文件)。

  3. 選擇 Create command or session (建立命令或工作階段)

  4. 輸入文件的描述性名稱

  5. (選用) 針對 Target type (目標類型),指定文件可在其上執行的資源類型。

  6. Document type (文件類型)清單中,選擇您要建立的文件類型。

  7. 刪除 Content (內容) 欄位中的括號,然後貼上先前建立的文件。

  8. (選用) 展開 Document tags (文件標籤) 區段,將一或多個標籤鍵值組套用至文件。

    標籤是您指派給資源的選用性中繼資料。標籤允許您以不同的方式 (例如用途、擁有者或環境) 將資源分類。例如,您可能想要標記文件來識別其執行的任務類型、目標作業系統類型以及其執行所在的環境。在這種情況下,您可以指定以下索引鍵名稱/值對:

    • Key=TaskType,Value=MyConfigurationUpdate

    • Key=OS,Value=AMAZON_LINUX_2

    • Key=Environment,Value=Production

    如需有關標記 Systems Manager 資源的詳細資訊,請參閱 標記 Systems Manager 資源

  9. 選擇 Create document (建立文件) 以儲存文件。

建立 SSM 文件 (命令列)

撰寫 SSM 文件內容 中所述,建立自訂 AWS Systems Manager (SSM) 文件的內容後,您可以使用 AWS Command Line Interface (AWS CLI) 或 AWS Tools for PowerShell,以您的內容建立 SSM 文件。下列命令顯示這種情況。

開始之前

如果您尚未安裝並設定 AWS CLI 或 AWS Tools for PowerShell,請進行相應的操作。如需相關資訊,請參閱安裝或更新 AWS CLI 的最新版本安裝 AWS Tools for PowerShell

執行下列命令。將每個範例資源預留位置取代為您自己的資訊。

Linux & macOS
aws ssm create-document \ --content file://path/to/file/documentContent.json \ --name "document-name" \ --document-type "Command" \ --tags "Key=tag-key,Value=tag-value"
Windows
aws ssm create-document ^ --content file://C:\path\to\file\documentContent.json ^ --name "document-name" ^ --document-type "Command" ^ --tags "Key=tag-key,Value=tag-value"
PowerShell
$json = Get-Content -Path "C:\path\to\file\documentContent.json" | Out-String New-SSMDocument ` -Content $json ` -Name "document-name" ` -DocumentType "Command" ` -Tags "Key=tag-key,Value=tag-value"

如果成功,此命令會傳回類似如下的回應。

{
"DocumentDescription":{
  "CreatedDate":1.585061751738E9,
  "DefaultVersion":"1",
  "Description":"MyCustomDocument",
  "DocumentFormat":"JSON",
  "DocumentType":"Command",
  "DocumentVersion":"1",
  "Hash":"0d3d879b3ca072e03c12638d0255ebd004d2c65bd318f8354fcde820dEXAMPLE",
  "HashType":"Sha256",
  "LatestVersion":"1",
  "Name":"Example",
  "Owner":"111122223333",
  "Parameters":[
     --truncated--
  ],
  "PlatformTypes":[
     "Windows",
     "Linux"
  ],
  "SchemaVersion":"0.3",
  "Status":"Creating",
  "Tags": [
        {
            "Key": "Purpose",
            "Value": "Test"
        }
    ]
}
}

建立 SSM 文件 (API)

撰寫 SSM 文件內容 中所述,建立自訂 AWS Systems Manager (SSM) 文件的內容後,您可以使用偏好的開發套件呼叫 AWS Systems Manager CreateDocument API 操作,以您的內容建立 SSM 文件。Content 請求參數的 JSON 或 YAML 字符串通常是從檔案中讀取。下面範例函數使用 Python、Go 和 Java 開發套件建立 SSM 文件。

Python
import boto3 ssm = boto3.client('ssm') filepath = '/path/to/file/documentContent.yaml' def createDocumentApiExample(): with open(filepath) as openFile: documentContent = openFile.read() createDocRequest = ssm.create_document( Content = documentContent, Name = 'createDocumentApiExample', DocumentType = 'Automation', DocumentFormat = 'YAML' ) print(createDocRequest) createDocumentApiExample()
Go
package main import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ssm" "fmt" "io/ioutil" "log" ) func main() { openFile, err := ioutil.ReadFile("/path/to/file/documentContent.yaml") if err != nil { log.Fatal(err) } documentContent := string(openFile) sesh := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable})) ssmClient := ssm.New(sesh) createDocRequest, err := ssmClient.CreateDocument(&ssm.CreateDocumentInput{ Content: &documentContent, Name: aws.String("createDocumentApiExample"), DocumentType: aws.String("Automation"), DocumentFormat: aws.String("YAML"), }) result := *createDocRequest fmt.Println(result) }
Java
import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Regions; import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement; import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder; import com.amazonaws.services.simplesystemsmanagement.model.*; public class createDocumentApiExample { public static void main(String[] args) { try { createDocumentMethod(getDocumentContent()); } catch (IOException e) { e.printStackTrace(); } } public static String getDocumentContent() throws IOException { String filepath = new String("/path/to/file/documentContent.yaml"); byte[] encoded = Files.readAllBytes(Paths.get(filepath)); String documentContent = new String(encoded, StandardCharsets.UTF_8); return documentContent; } public static void createDocumentMethod (final String documentContent) { AWSSimpleSystemsManagement ssm = AWSSimpleSystemsManagementClientBuilder.defaultClient(); final CreateDocumentRequest createDocRequest = new CreateDocumentRequest() .withContent(documentContent) .withName("createDocumentApiExample") .withDocumentType("Automation") .withDocumentFormat("YAML"); final CreateDocumentResult result = ssm.createDocument(createDocRequest); } }

如需有關建立自訂文件內容的詳細資訊,請參閱資料元素和參數

刪除自訂 SSM 文件

如果您不想再使用自訂 SSM 文件,您可使用 AWS Command Line Interface (AWS CLI) 或 AWS Systems Manager 主控台刪除它。

若要刪除 SSM 文件 (AWS CLI)
  1. 刪除文件之前,建議您取消與文件相關聯的所有執行個體的關聯。

    執行以下命令來取消執行個體與文件的關聯。

    aws ssm delete-association --instance-id "123456789012" --name "documentName"

    如果命令成功,則無輸出訊息。

  2. 執行下列命令。將每個範例資源預留位置取代為您自己的資訊。

    Linux
    aws ssm delete-document \ --name "document-name" \ --document-version "document-version" \ --version-name "version-name"
    Windows
    aws ssm delete-document ^ --name "document-name" ^ --document-version "document-version" ^ --version-name "version-name"
    PowerShell
    Delete-SSMDocument ` -Name "document-name" ` -DocumentVersion 'document-version' ` -VersionName 'version-name'

    如果命令成功,則無輸出訊息。

    重要

    如果未提供 document-versionversion-name,則會刪除文件的所有版本。

若要刪除 SSM 文件 (主控台)
  1. 開啟位於 https://console.aws.amazon.com/systems-manager/ 的 AWS Systems Manager 主控台。

  2. 在導覽窗格中,選擇 Documents (文件)

    -或-

    如果 AWS Systems Manager 首頁先開啟,選擇選單圖示 ( 
    The menu icon
  ) 以開啟導覽窗格,然後在導覽窗格中,選擇 Documents (文件)。

  3. 選取您要刪除的文件。

  4. 選取 Delete (刪除)。當提示您刪除文件時,請選取 Delete (刪除)。