你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
将计算机配置为所需状态
使用 Azure 自动化状态配置可以指定服务器配置,并确保这些服务器在一段时间后处于指定状态。
- 登记要由 Azure Automation DSC 管理的 VM
- 将配置上传到 Azure 自动化
- 将配置编译为节点配置
- 将节点配置分配给托管节点
- 检查托管节点的符合性状态
在本教程中,我们使用简单 DSC 配置,以确保在 VM 上安装 IIS。
先决条件
- 一个 Azure 自动化帐户。 若要了解有关 Azure 自动化帐户及其要求的更多信息,请参阅自动化帐户身份验证概述。
- 一个运行 Windows Server 2008 R2 或更高版本的 Azure 资源管理器 VM(非经典)。 如需创建 VM 的说明,请参阅在 Azure 门户中创建第一个 Windows 虚拟机。
- Azure PowerShell 模块 3.6 版或更高版本。 运行
Get-Module -ListAvailable Az
即可查找版本。 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。 - 熟悉所需状态配置 (DSC)。 有关 DSC 文档的信息,请参阅 Windows PowerShell Desired State Configuration 概述。
对部分配置的支持
Azure Automation State Configuration 支持使用部分配置。 在这种情况下,DSC 配置为独立管理多个配置,并从 Azure 自动化检索每个配置。 但是,每个自动化帐户只能向节点分配一个配置。 这意味着,如果对一个节点使用两个配置,则需要两个自动化帐户。
有关如何从请求服务注册部分配置的详细信息,请参阅部分配置的文档。
如需深入了解团队如何使用配置即代码协作管理服务器,请参阅了解 DSC 在 CI/CD 管道中的角色。
登录 Azure
使用 Connect-AzAccount cmdlet 登录到 Azure 订阅,然后按屏幕说明操作。
Connect-AzAccount
创建配置并将配置上传到 Azure 自动化
在文本编辑器中键入以下内容,并在本地将文件保存为 TestConfig.ps1。
configuration TestConfig {
Node WebServer {
WindowsFeature IIS {
Ensure = 'Present'
Name = 'Web-Server'
IncludeAllSubFeature = $true
}
}
}
注意
Azure 自动化中的配置名称必须限制为不超过 100 个字符。
在需要导入多个提供 DSC 资源的模块的更高级方案中,请确保每个模块在配置中具有唯一 Import-DscResource
行。
调用 Import-AzAutomationDscConfiguration cmdlet,将配置上传到自动化帐户。
Import-AzAutomationDscConfiguration -SourcePath 'C:\DscConfigs\TestConfig.ps1' -ResourceGroupName 'MyResourceGroup' -AutomationAccountName 'myAutomationAccount' -Published
将配置编译为节点配置
必须先将 DSC 配置编译为节点配置,然后才能将它分配给节点。 参阅 DSC 配置。
调用 Start-AzAutomationDscCompilationJob cmdlet,将 TestConfig
配置编译为自动化帐户中名为 TestConfig.WebServer
的节点配置。
Start-AzAutomationDscCompilationJob -ConfigurationName 'TestConfig' -ResourceGroupName 'MyResourceGroup' -AutomationAccountName 'myAutomationAccount'
注册要由状态配置管理的 VM
可以使用 Azure 自动化状态配置来管理 Azure VM(包括经典 VM 和资源管理器 VM)、本地 VM、Linux 计算机、AWS VM,以及本地物理机。 在本主题中,我们介绍如何仅注册 Azure 资源管理器 VM。 有关注册其他类型的计算机的信息,请参阅登记由 Azure 自动化状态配置管理的计算机。
调用 Register-AzAutomationDscNode cmdlet,将 VM 作为托管节点注册到 Azure Automation State Configuration。
Register-AzAutomationDscNode -ResourceGroupName 'MyResourceGroup' -AutomationAccountName 'myAutomationAccount' -AzureVMName 'DscVm'
指定配置模式设置
使用 Register-AzAutomationDscNode cmdlet,将 VM 注册为托管节点并指定配置属性。 例如,可以通过指定 ApplyOnly
作为 ConfigurationMode
属性的值,指定计算机的状态仅应用一次。 State Configuration 不会尝试在初始检查后应用配置。
Register-AzAutomationDscNode -ResourceGroupName 'MyResourceGroup' -AutomationAccountName 'myAutomationAccount' -AzureVMName 'DscVm' -ConfigurationMode 'ApplyOnly'
还可使用 ConfigurationModeFrequencyMins
属性指定 DSC 检查配置状态的频率。 有关 DSC 配置设置的详细信息,请参阅配置本地配置管理器。
# Run a DSC check every 60 minutes
Register-AzAutomationDscNode -ResourceGroupName 'MyResourceGroup' -AutomationAccountName 'myAutomationAccount' -AzureVMName 'DscVm' -ConfigurationModeFrequencyMins 60
将节点配置分配给托管节点
现在我们可以将已编译的节点配置分配给我们想要配置的 VM。
# Get the ID of the DSC node
$node = Get-AzAutomationDscNode -ResourceGroupName 'MyResourceGroup' -AutomationAccountName 'myAutomationAccount' -Name 'DscVm'
# Assign the node configuration to the DSC node
Set-AzAutomationDscNode -ResourceGroupName 'MyResourceGroup' -AutomationAccountName 'myAutomationAccount' -NodeConfigurationName 'TestConfig.WebServer' -NodeId $node.Id
这会将名为 TestConfig.WebServer
的节点配置分配到已注册 DSC 节点 DscVm
。 默认情况下,每隔 30 分钟会检查一次 DSC 节点是否符合节点配置。 有关如何更改符合性检查间隔的信息,请参阅配置本地配置管理器。
检查托管节点的符合性状态
可通过使用 Get-AzAutomationDscNodeReport cmdlet 来获取有关托管节点符合性状态的报告。
# Get the ID of the DSC node
$node = Get-AzAutomationDscNode -ResourceGroupName 'MyResourceGroup' -AutomationAccountName 'myAutomationAccount' -Name 'DscVm'
# Get an array of status reports for the DSC node
$reports = Get-AzAutomationDscNodeReport -ResourceGroupName 'MyResourceGroup' -AutomationAccountName 'myAutomationAccount' -NodeId $node.Id
# Display the most recent report
$reports[0]
后续步骤
- 有关入门信息,请参阅 Azure Automation State Configuration 入门。
- 要了解如何启用节点,请参阅启用 Azure Automation State Configuration。
- 若要了解如何编译 DSC 配置,以便将它们分配给目标节点,请参阅在 Azure Automation State Configuration 中编译 DSC 配置。
- 若要查看在持续部署管道中使用 Azure Automation State Configuration 的示例,请参阅使用 Chocolatey 设置持续部署。
- 有关定价信息,请参阅 Azure Automation State Configuration 定价。
- 有关 PowerShell cmdlet 参考,请参阅 Az.Automation。