Writing better DSC Configurations with Configuration Data
Configuration Data in Windows PowerShell Desired State Configuration (DSC) allows you to separate the what from the where. Configuration Data enables you to write better DSC configuration. Configuration Data is defined as a Hash table and is passed in when the configuration is compiled. If you're using Automation DSC on Azure, that looks like this:
As you can see on the last line, the configuration data is imported as part of the Automation DSC compilation job.
The basic layout of a Configuration Data file is:
Each Node entry must have a NodeName property as this property is used to generate a MOF file for each node in the AllNodes array.
When you import a Configuration Data file, new variables are available to you when defining your configuration. These variables allow you to define more sophisticated and succinct configurations. The variables are:
- $AllNodes - Refers to the AllNodes array, use this variable with .Where() and .Foreach()
- $Node - Refers to the current node within the AllNodes array once the array has been filtered
- $ConfigurationData - This refers to the entire configuration data file hash table
You can use AllNodes.Where() to select specific nodes, for example, let's say we have a node that has a property of Role, and that role is defined as either DomainController or FileServer. Let's use the below configuration data as an example:
When you compile the above configuration, with the configuration data, you will get two MOF files, one for DC1 and one for FS1. The DC1 configuration file will only have settings for the DNS and NTDS services, the FS1 MOF file will only have settings for a File on D:\.
You can go one step further than this, by using the $ConfigurationData variable to access data outside of the $AllNodes block. Let's look at the below example:
In the above example, the configuration will loop through each of the services defined on a role and add them to the configuration.
Now let's assume that you have heaps of software, features, services and other settings you need to deploy to each of your nodes. You're going to start saving lines in your configuration, and eliminating code that would otherwise be repeated.