Interacting With Windows Registry Using Chef
One of the most well-known differences between managing UNIX-like systems and Windows systems is the Windows Registry. Chef has resources for creating, modifying, and deleting Windows Registry keys. Beware that these operations are nonreversible (there is no implicit backup of values, so it may be worth preparing a backup before modifying values), and that they can potentially be very destructive.
Paths to registry keys must also include the registry hive. The hive can be fully specified or we could use the following abbreviations:
- HKLM for HKEY_LOCAL_MACHINE
- HKCC for HKEY_CURRENT_CONFIG
- HKCR for HKEY_CLASSES_ROOT
- HKU for HKEY_USERS
- HKCU for HKEY_CURRENT_USER
Note- Chef 10.x uses a resource named windows_registry, which will be described here for those using an older Chef client and server. For newer install versions using 11.x, the resource is registry_key and is the preferred way to interact with Registry values when using a newer version of Chef. Use the one that corresponds with your Chef version.
The Chef 10.x resource
The windows_registry resource allows you to control registry settings but has different actions and attributes than the registry_key resource. Moving from windows_registry to registry_key should be a fairly straightforward operation when it is time to migrate. The following table provides a description of the actions and parameters used with the windows_registry resource:
Examples of managing registry keys
Manage NTP servers through the registry to allow off-site NTP synchronization with the NTP pool and update every 45 minutes until you have performed three good syncs, and then once every 8 hours with the following code:
regkey = 'HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Parameter'
windows_registry regkey do
values 'AvoidTimeSyncOnWan' => 0,
'NtpServer' => "0.pool.ntp.org",
'Period' => 'SpecialSkew'
end
As shown in the following code, delete two specific key/value pairs from the registry at a given locationsetting the value to blank achieves the following result:
windows_registry 'HKCU\Software\SomeApp\SomeKey' do
values 'UnwantedValueOne' => '',
'UnwantedValueTwo' => ''
action :remove
end
There are also some helper methods available for determining if keys exist and/ or fetching the data from the registry. These come in handy when writing guards around other blocks or for extracting registry data for use in other resources.
Use the following code to determine if a value exists:
Windows::RegistryHelper.value_exists?(path, value)
Use the following code to check to see if a key exists:
Windows::RegistryHelper.key_exists?(path)
Use the following code to get the value from a registry key:
result = Windows::RegistryHelper.get_value(path, value)
Chef 0.11.x resource
In the newer versions of Chef, the registry_key resource is used for setting values in the Windows Registry, as described in the following table:
Examples of managing Registry values
To set the NTP configuration the same way that would have been done with the windows_registry resource, a matching registry_key resource would look like the following code:
regkey = 'HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Parameter'
registry_key regkey do
values [{
:name => 'AvoidTimeSyncOnWan',
:type => :reg_dword,
:data => 0
}, {
:name => 'NtpServer',
:data => "0.pool.ntp.org",
}, {
:name => 'Period',
:type => :reg_string,
:data => 'SpecialSkew'
},]
action :create
end