Configuring Virtual Machines Using Desired State Configuration - Part 2 - Local Configuration Manager
In the previous post of the series (available here) we went through an introduction of Powershell DSC and how it can help with configuring virtual machines. This post is going to be about the Local Configuration Manager, also known as LCM, the engine that is the heart of DSC.
When a configuration file is pushed to a node, the LCM is the component that is responsible for performing all the operations in order to bring the system to the desired state. Apart from that, LCM can also monitor the system for configuration drift and take actions in order to remedy it.
LCM works with files in the C:\Windows\System32\Configuration directory and there are three of them in particular that you should be aware of:
Current.mof. This is the file that contains the current configuration.
Previous.mof. This file contains the previous configuration that was applied to the system. Usefull for reverting to a previous state.
The following screenshot shows the above files:
Pending.mof. When a configuration is in progress and has not yet been fully applied, it is saved with this name, as shown below:
There are times that LCM may face issues when trying to apply a configuration, like for example when getting a file from a file server that is not accessible. If you try to apply a different configuration you most probably get a message similar to the below:
The way to get around it is to remove the pending configuration file and then give the configuration another try.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [DSCLocalConfigurationManager()] configuration LCMConfig { Node localhost { Settings { RefreshMode = 'Push' ConfigurationMode = 'ApplyAndMonitor' RebootNodeIfNeeded = $true RefreshFrequencyMins = 30 ConfigurationModeFrequencyMins = 15 } } } |
The attribute at line 1 is a special attribute that is used in order to mark this configuration as LCM related.
Out of the many properties that LCM configurations contain, I've picked five of them that I believe are the most important:
RefreshMode: The way that a configuration is assigned to a node. There are three options regarding the refresh mode:
- Push: The configuration is pushed to the node using the Start-DscConfiguration cmdlet.
- Pull: The node pulls configurations from a DSC pull service.
- Disabled: LCM does not process any configuration.
ConfigurationMode: There are three configuration modes available:
- ApplyOnly: LCM applies the configuration and does nothing else.
- ApplyAndMonitor: LCM applies the configuration and when configuration drift is detected it reports it.
- ApplyAndAutoCorrect: LCM applies the configuration and when configuration drift is detected it re-applies the configuration.
RebootNodeIfNeeded: Control whether LCM will reboot the node if requested by a resource.
RefreshFrequencyMins: The interval at which LCM checks for new configurations published on the DSC pull service - only applies when the LCM refresh mode is set to pull.
ConfigurationModeFrequencyMins: The interval between two configuration checks - ignored if configuration mode is set to "ApplyOnly".
To apply the LCM configuration you have to load and compile it as any other configuration file but in order to apply it you have to use the Set-DscLocalConfigurationManager cmdlet like below:
The Get-DscLocalConfigurationManager cmdlet displays the configuration of LCM:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [DSCLocalConfigurationManager()] configuration LCMClear { Node localhost { Settings { RefreshMode = 'Push' ConfigurationMode = 'ApplyOnly' RebootNodeIfNeeded = $false RefreshFrequencyMins = 30 ConfigurationModeFrequencyMins = 15 } } } |