演练:使用 CLI 配置 Inventory 的托管式节点 - Amazon Systems Manager
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

演练:使用 CLI 配置 Inventory 的托管式节点

以下过程为您演示了将 Amazon Systems Manager Inventory 配置为从托管式节点收集元数据的过程。在配置清单收集时,应该首先创建 Systems Manager State Manager 关联。Systems Manager 将在关联运行时收集清单数据。如果您不首先创建关联,并尝试使用 Systems Manager Run Command(举例来说)调用 aws:softwareInventory 插件,则系统将返回以下错误:

The aws:softwareInventory plugin can only be invoked via ssm-associate.

注意

一个节点一次只能配置一个清单关联。如果您为一个节点配置了两个或更多清单关联,则关联不会运行,而且系统不会收集清单数据。

快速配置 Inventory 的所有托管式节点 (CLI)

您可以在您的 Amazon Web Services 账户 和当前区域中快速配置所有托管式节点来收集清单数据。这称为创建全局清单关联。要使用 Amazon CLI 创建全局清单关联,请为 instanceIds 值使用通配符选项,如以下过程所示。

在您的 Amazon Web Services 账户 和当前区域中为所有托管式节点配置清单 (CLI)
  1. 安装并配置 Amazon Command Line Interface (Amazon CLI)(如果尚未执行该操作)。

    有关信息,请参阅安装或更新 Amazon CLI 的最新版本

  2. 运行以下命令。

    Linux & macOS
    aws ssm create-association \ --name AWS-GatherSoftwareInventory \ --targets Key=InstanceIds,Values=* \ --schedule-expression "rate(1 day)" \ --parameters applications=Enabled,awsComponents=Enabled,customInventory=Enabled,instanceDetailedInformation=Enabled,networkConfig=Enabled,services=Enabled,windowsRoles=Enabled,windowsUpdates=Enabled
    Windows
    aws ssm create-association ^ --name AWS-GatherSoftwareInventory ^ --targets Key=InstanceIds,Values=* ^ --schedule-expression "rate(1 day)" ^ --parameters applications=Enabled,awsComponents=Enabled,customInventory=Enabled,instanceDetailedInformation=Enabled,networkConfig=Enabled,services=Enabled,windowsRoles=Enabled,windowsUpdates=Enabled
注意

此命令不允许 Inventory 收集 Windows 注册表或文件的元数据。要为这些数据类型启用清单,请使用下一个过程。

在托管式节点上手动配置 Inventory (CLI)

通过以下过程,使用节点 ID 或标签在您的托管式节点上手动配置 Amazon Systems Manager Inventory。

手动配置 Inventory 的托管式节点 (CLI)
  1. 安装并配置 Amazon Command Line Interface (Amazon CLI)(如果尚未执行该操作)。

    有关信息,请参阅安装或更新 Amazon CLI 的最新版本

  2. 运行以下命令,创建一个在节点上运行 Systems Manager Inventory 的 State Manager 关联。将每个示例资源占位符替换为您自己的信息。此命令将服务配置为每六小时运行一次,并从节点收集网络配置、Windows 更新和应用程序元数据。

    Linux & macOS
    aws ssm create-association \ --name "AWS-GatherSoftwareInventory" \ --targets "Key=instanceids,Values=an_instance_ID" \ --schedule-expression "rate(240 minutes)" \ --output-location "{ \"S3Location\": { \"OutputS3Region\": \"region_ID, for example us-east-2\", \"OutputS3BucketName\": \"DOC-EXAMPLE-BUCKET\", \"OutputS3KeyPrefix\": \"Test\" } }" \ --parameters "networkConfig=Enabled,windowsUpdates=Enabled,applications=Enabled"
    Windows
    aws ssm create-association ^ --name "AWS-GatherSoftwareInventory" ^ --targets "Key=instanceids,Values=an_instance_ID" ^ --schedule-expression "rate(240 minutes)" ^ --output-location "{ \"S3Location\": { \"OutputS3Region\": \"region_ID, for example us-east-2\", \"OutputS3BucketName\": \"DOC-EXAMPLE-BUCKET\", \"OutputS3KeyPrefix\": \"Test\" } }" ^ --parameters "networkConfig=Enabled,windowsUpdates=Enabled,applications=Enabled"

    系统会使用类似以下形式的信息进行响应。

    {
        "AssociationDescription": {
            "ScheduleExpression": "rate(240 minutes)",
            "OutputLocation": {
                "S3Location": {
                    "OutputS3KeyPrefix": "Test",
                    "OutputS3BucketName": "Test bucket",
                    "OutputS3Region": "us-east-2"
                }
            },
            "Name": "The name you specified",
            "Parameters": {
                "applications": [
                    "Enabled"
                ],
                "networkConfig": [
                    "Enabled"
                ],
                "windowsUpdates": [
                    "Enabled"
                ]
            },
            "Overview": {
                "Status": "Pending",
                "DetailedStatus": "Creating"
            },
            "AssociationId": "1a2b3c4d5e6f7g-1a2b3c-1a2b3c-1a2b3c-1a2b3c4d5e6f7g",
            "DocumentVersion": "$DEFAULT",
            "LastUpdateAssociationDate": 1480544990.06,
            "Date": 1480544990.06,
            "Targets": [
                {
                    "Values": [
                       "i-02573cafcfEXAMPLE"
                    ],
                    "Key": "InstanceIds"
                }
            ]
        }
    }

    您可以搭配使用 Targets 参数和 EC2 标签来确定大型目标节点组。请参阅以下示例。

    Linux & macOS
    aws ssm create-association \ --name "AWS-GatherSoftwareInventory" \ --targets "Key=tag:Environment,Values=Production" \ --schedule-expression "rate(240 minutes)" \ --output-location "{ \"S3Location\": { \"OutputS3Region\": \"us-east-2\", \"OutputS3BucketName\": \"DOC-EXAMPLE-BUCKET\", \"OutputS3KeyPrefix\": \"Test\" } }" \ --parameters "networkConfig=Enabled,windowsUpdates=Enabled,applications=Enabled"
    Windows
    aws ssm create-association ^ --name "AWS-GatherSoftwareInventory" ^ --targets "Key=tag:Environment,Values=Production" ^ --schedule-expression "rate(240 minutes)" ^ --output-location "{ \"S3Location\": { \"OutputS3Region\": \"us-east-2\", \"OutputS3BucketName\": \"DOC-EXAMPLE-BUCKET\", \"OutputS3KeyPrefix\": \"Test\" } }" ^ --parameters "networkConfig=Enabled,windowsUpdates=Enabled,applications=Enabled"

    您还可以搭配使用 fileswindowsRegistry 清单类型与表达式来清点 Windows Server 节点上的清单文件和 Windows 注册表项。有关这些清单类型的更多信息,请参阅 使用文件和 Windows 注册表清单

    Linux & macOS
    aws ssm create-association \ --name "AWS-GatherSoftwareInventory" \ --targets "Key=instanceids,Values=i-0704358e3a3da9eb1" \ --schedule-expression "rate(240 minutes)" \ --parameters '{"files":["[{\"Path\": \"C:\\Program Files\", \"Pattern\": [\"*.exe\"], \"Recursive\": true}]"], "windowsRegistry": ["[{\"Path\":\"HKEY_LOCAL_MACHINE\\Software\\Amazon\", \"Recursive\":true}]"]}' \ --profile dev-pdx
    Windows
    aws ssm create-association ^ --name "AWS-GatherSoftwareInventory" ^ --targets "Key=instanceids,Values=i-0704358e3a3da9eb1" ^ --schedule-expression "rate(240 minutes)" ^ --parameters '{"files":["[{\"Path\": \"C:\\Program Files\", \"Pattern\": [\"*.exe\"], \"Recursive\": true}]"], "windowsRegistry": ["[{\"Path\":\"HKEY_LOCAL_MACHINE\\Software\\Amazon\", \"Recursive\":true}]"]}' ^ --profile dev-pdx
  3. 运行以下命令查看关联状态。

    aws ssm describe-instance-associations-status --instance-id an_instance_ID

    系统会使用类似以下形式的信息进行响应。

    {
    "InstanceAssociationStatusInfos": [
             {
                "Status": "Pending",
                "DetailedStatus": "Associated",
                "Name": "reInvent2016PolicyDocumentTest",
                "InstanceId": "i-1a2b3c4d5e6f7g",
                "AssociationId": "1a2b3c4d5e6f7g-1a2b3c-1a2b3c-1a2b3c-1a2b3c4d5e6f7g",
                "DocumentVersion": "1"
            }
    ]
    }