Ah! I always struggle to explain these concepts and the superiority of Policy-Driven, but I think I’ve found a good example! My Air Conditioning systems…
DSC
Another common challenge with DSC, is how to compose DSC configurations.
People have seen the trick of having a Configuration, and the following code within:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Configuration MyDSCConfig { | |
Node $ConfigurationData.AllNodes.nodename { | |
if($Node.roles -contains 'MyRole') { | |
# do stuff here, like calling DSC Resources | |
MyCompositeResource StuffForMyRole { | |
# … | |
} | |
} | |
if($Node.roles -contains 'MyOtherRole') { | |
MyOtherResource OtherStuff { | |
# … | |
} | |
} | |
} | |
} |
This is a good way to get started and works well for small-ish configurations, but it gets out of hand pretty quickly, as it’s hard to read all the if
statements and their content. Some variant of this are using Where
clauses around the Node expression.
I’m writing and using a PowerShell module called “Datum” to manage DSC Configuration Data, and raise cattle at work. This is a write up to explain the problem, and why I’m writing it.
I assume you already know a bit about Desired State Configuration, but if it’s new to you or vague, go grab yourself The DSC Book by Don Jones & Missy Januszko, which packs all you need to know about out-of-the-box DSC, and more.
This post tries to explain the DSC Configuration Data composition challenge that I attempt to solve with Datum. It does not include the solution and how to use Datum to solve those problems: you can look on the GitHub repository and see an example of a Control Repository, or come to the PowerShell Summit 2018, where I’ll be presenting Datum used in a DSC Infrastructure.
Here’s a quick function to allow you something close to splatting with DSC resources:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-DscSplattedResource { | |
[CmdletBinding()] | |
Param( | |
[String] | |
$ResourceName, | |
[String] | |
$ExecutionName, | |
[hashtable] | |
$Properties | |
) | |
$stringBuilder = [System.Text.StringBuilder]::new() | |
$null = $stringBuilder.AppendLine("Param([hashtable]`$Parameters)") | |
$null = $stringBuilder.AppendLine(" $ResourceName $ExecutionName { ") | |
foreach($PropertyName in $Properties.keys) { | |
$null = $stringBuilder.AppendLine("$PropertyName = `$(`$Parameters['$PropertyName'])") | |
} | |
$null = $stringBuilder.AppendLine("}") | |
Write-Debug ("Generated Resource Block = {0}" -f $stringBuilder.ToString()) | |
[scriptblock]::Create($stringBuilder.ToString()).Invoke($Properties) | |
} | |
Set-Alias –Name x –Value Get-DscSplattedResource |
Here’s why you may need it.