适用于:Windows PowerShell 4.0、Windows PowerShell 5.0
使用配置数据将 DSC 配置中使用的数据与配置本身分开可能很有用。 通过这样做,您可以对多个环境使用单个配置。
例如,如果您正在开发应用程序,则可以对开发环境和生产环境使用一个配置,并使用配置数据为每个环境指定数据。
什么是配置数据?
配置数据是在哈希表中定义并在编译该配置时传递给 DSC 配置的数据。
有关 ConfigurationData 哈希表的详细说明,请参阅 使用配置数据。
一个简单的例子
让我们看一个非常简单的例子,看看它是如何工作的。 我们将创建一个配置,以确保 IIS 存在于某些节点上,而 Hyper-V 存在于其他节点上:
Configuration MyDscConfiguration {
Node $AllNodes.Where{$_.Role -eq "WebServer"}.NodeName
{
WindowsFeature IISInstall {
Ensure = 'Present'
Name = 'Web-Server'
}
}
Node $AllNodes.Where{$_.Role -eq "VMHost"}.NodeName
{
WindowsFeature HyperVInstall {
Ensure = 'Present'
Name = 'Hyper-V'
}
}
}
$MyData =
@{
AllNodes =
@(
@{
NodeName = 'VM-1'
Role = 'WebServer'
},
@{
NodeName = 'VM-2'
Role = 'VMHost'
}
)
}
MyDscConfiguration -ConfigurationData $MyData
此脚本中的最后一行编译配置,作为值 ConfigurationData 参数传递$MyData。
结果是创建了两个 MOF 文件:
Directory: C:\DscTests\MyDscConfiguration
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/31/2017 5:09 PM 1968 VM-1.mof
-a---- 3/31/2017 5:09 PM 1970 VM-2.mof
$MyData 指定两个不同的节点,每个节点都有自己的 NodeName 和 Role。 该配置通过获取它从中$MyData获取的节点集合(特别是$AllNodes)来动态创建 Node 块,并根据属性筛选Role该集合。
使用配置数据定义开发和生产环境
让我们看一个完整的示例,该示例使用单个配置来设置网站的开发和生产环境。 在开发环境中,IIS 和 SQL Server 都安装在单个节点上。 在生产环境中,IIS 和 SQL Server 安装在单独的节点上。 我们将使用配置数据 .psd1 文件来指定两个不同环境的数据。
配置文件
我们将在名为 DevProdEnvData.psd1 以下的文件中定义开发和生产环境数据:
@{
AllNodes = @(
@{
NodeName = "*"
SQLServerName = "MySQLServer"
SqlSource = "C:\Software\Sql"
DotNetSrc = "C:\Software\sxs"
WebSiteName = "New website"
},
@{
NodeName = "Prod-SQL"
Role = "MSSQL"
},
@{
NodeName = "Prod-IIS"
Role = "Web"
SiteContents = "C:\Website\Prod\SiteContents\"
SitePath = "\\Prod-IIS\Website\"
},
@{
NodeName = "Dev"
Role = "MSSQL", "Web"
SiteContents = "C:\Website\Dev\SiteContents\"
SitePath = "\\Dev\Website\"
}
)
}
配置文件脚本文件
现在,在文件中.ps1定义的配置中,我们按其角色(、 Dev或两者)MSSQL过滤我们定义的DevProdEnvData.psd1节点,并相应地配置它们。 开发环境在一个节点上同时具有 SQL Server 和 IIS,而生产环境在两个不同的节点上具有它们。 网站内容也不同,如属性所 SiteContents 指定。
在配置脚本的末尾,我们调用配置(将其编译成 MOF 文档), DevProdEnvData.psd1 作为参数传递 $ConfigurationData 。
注意:此配置需要模块
xSqlPsxWebAdministration并安装在目标节点上。
让我们在名为 MyWebApp.ps1: 的文件中定义配置:
Configuration MyWebApp
{
Import-DSCResource -ModuleName PSDesiredStateConfiguration
Import-DSCResource -ModuleName xSqlPs
Import-DSCResource -ModuleName xWebAdministration
Node $AllNodes.Where{$_.Role -contains "MSSQL"}.NodeName
{
# Install prerequisites
WindowsFeature installdotNet35
{
Ensure = "Present"
Name = "Net-Framework-Core"
Source = "c:\software\sxs"
}
# Install SQL Server
xSqlServerInstall InstallSqlServer
{
InstanceName = $Node.SQLServerName
SourcePath = $Node.SqlSource
Features = "SQLEngine,SSMS"
DependsOn = "[WindowsFeature]installdotNet35"
}
}
Node $AllNodes.Where{$_.Role -contains "Web"}.NodeName
{
# Install the IIS role
WindowsFeature IIS
{
Ensure = 'Present'
Name = 'Web-Server'
}
# Install the ASP .NET 4.5 role
WindowsFeature AspNet45
{
Ensure = 'Present'
Name = 'Web-Asp-Net45'
}
# Stop the default website
xWebsite DefaultSite
{
Ensure = 'Present'
Name = 'Default Web Site'
State = 'Stopped'
PhysicalPath = 'C:\inetpub\wwwroot'
DependsOn = '[WindowsFeature]IIS'
}
# Copy the website content
File WebContent
{
Ensure = 'Present'
SourcePath = $Node.SiteContents
DestinationPath = $Node.SitePath
Recurse = $true
Type = 'Directory'
DependsOn = '[WindowsFeature]AspNet45'
}
# Create the new Website
xWebsite NewWebsite
{
Ensure = 'Present'
Name = $Node.WebSiteName
State = 'Started'
PhysicalPath = $Node.SitePath
DependsOn = '[File]WebContent'
}
}
}
MyWebApp -ConfigurationData DevProdEnvData.psd1
运行此配置时,将创建三个 MOF 文件( AllNodes 数组中的每个命名条目一个):
Directory: C:\DscTests\MyWebApp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/31/2017 5:47 PM 2944 Prod-SQL.mof
-a---- 3/31/2017 5:47 PM 6994 Dev.mof
-a---- 3/31/2017 5:47 PM 5338 Prod-IIS.mof
使用非节点数据
您可以向 ConfigurationData 哈希表添加其他键,以获取不特定于节点的数据。 以下配置可确保存在两个网站。 每个网站的数据在 AllNodes 数组中定义。 该文件 Config.xml 用于这两个网站,因此我们在具有名称 NonNodeData的附加键中定义它。 请注意,您可以根据需要拥有任意数量的其他键,并且可以将它们命名为任何您想要的名称。
NonNodeData 不是一个保留词,它只是我们决定命名附加键的。
您可以使用特殊变量 $ConfigurationData 访问其他密钥。 在此示例中, ConfigFileContents 通过以下行访问:
Contents = $ConfigurationData.NonNodeData.ConfigFileContents
在资源块中 File 。
$MyData =
@{
AllNodes =
@(
@{
NodeName = "*"
LogPath = "C:\Logs"
},
@{
NodeName = "VM-1"
SiteContents = "C:\Site1"
SiteName = "Website1"
},
@{
NodeName = "VM-2"
SiteContents = "C:\Site2"
SiteName = "Website2"
}
);
NonNodeData =
@{
ConfigFileContents = (Get-Content C:\Template\Config.xml)
}
}
configuration WebsiteConfig
{
Import-DscResource -ModuleName xWebAdministration -Name MSFT_xWebsite
node $AllNodes.NodeName
{
xWebsite Site
{
Name = $Node.SiteName
PhysicalPath = $Node.SiteContents
Ensure = "Present"
}
File ConfigFile
{
DestinationPath = $Node.SiteContents + "\\config.xml"
Contents = $ConfigurationData.NonNodeData.ConfigFileContents
}
}
}