Nested DSC configuration syntax

Think of using nested DSC configurations as writing PowerShell functions. Each PowerShell function encompasses a certain purpose just like a DSC configuration block encompasses a certain set of configuration instructions. If we look at the example in code_example_05.ps1, we can see that setting the environment variable and creating the config file are related and tied together. If we encapsulate them together, we can just reference a single block of code instead of a list of DSC resources. Let's use code_example_5.ps1 and apply the nested DSC configuration method to it:

[CmdletBinding()]
param(
[Parameter()]$OutputPath = [IO.Path]::Combine($PSScriptRoot, 'InstallExampleSoftware'),
[Parameter()]$ConfigData
)

Configuration ImportantSoftwareConfig
{
param($ConfigFile, $ConfigFileContents)

Environment AppEnvVariable
{
Ensure = 'Present'
Name = 'ConfigFileLocation'
Value = $ConfigFile
}

File ConfigFile
{
DestinationPath = $ConfigFile
Contents = $ConfigFileContents
}
}

Configuration InstallExampleSoftware
{
Node $AllNodes.NodeName
{

WindowsFeature DotNet
{
Ensure = 'Present'
Name = 'NET-Framework-45-Core'
}
}

Node $AllNodes.Where({$_.Roles -contains 'FooBar'}).NodeName
{
ImportantSoftwareConfig SetImportantValues
{
ConfigFile = $Node.ExampleSoftware.ConfigFile
ConfigFileContents = $ConfigurationData.NonNodeData.ConfigFileContents
}

Package InstallExampleSoftware
{
Ensure = 'Present'
Name = $Node.ExampleSoftware.Name
ProductId = $Node.ExampleSoftware.ProductId
Path = $Node.ExampleSoftware.SourcePath
DependsOn = @('[WindowsFeature]DotNet')
}
}
}
InstallExampleSoftware -OutputPath $OutputPath -ConfigurationData $ConfigData

We immediately see the benefit of encapsulating a set of configuration instructions inside separate DSC configurations when we examine InstallExampleSoftware. All the work needed to get the important environment variable set is encapsulated inside the ImportantSoftwareConfig nested DSC configuration, and the main InstallExampleSoftware is able to deal with the rest of the configuration. We now clearly see that setting the environment variable and creating the config file are related and dependent on each other, and we can reference this block wherever we need to. It is now a reusable piece of code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset