dsc_resource
Windows PowerShell is a task-based command-line shell and scripting language developed by Microsoft. Windows PowerShell uses a document-oriented approach for managing Microsoft Windows-based machines, similar to the approach that is used for managing UNIX- and Linux-based machines. Windows PowerShell is a tool-agnostic platform that supports using Chef for configuration management.
Desired State Configuration (DSC) is a feature of Windows PowerShell that provides a set of language extensions, cmdlets, and resources that can be used to declaratively configure software. DSC is similar to Chef, in that both tools are idempotent, take similar approaches to the concept of resources, describe the configuration of a system, and then take the steps required to do that configuration. The most important difference between Chef and DSC is that Chef uses Ruby and DSC is exposed as configuration data from within Windows PowerShell.
The dsc_resource resource allows any DSC resource to be used in a Chef recipe, as well as any custom resources that have been added to your Windows PowerShell environment. Microsoft frequently adds new resources to the DSC resource collection.
Warning
Using the dsc_resource has the following requirements:
-
Windows Management Framework (WMF) 5.0 February Preview (or higher), which includes Windows PowerShell 5.0.10018.0 (or higher).
-
The
RefreshModeconfiguration setting in the Local Configuration Manager must be set toDisabled.NOTE: Starting with the chef-client 12.6 release, this requirement applies only for versions of Windows PowerShell earlier than 5.0.10586.0. The latest version of Windows Management Framework (WMF) 5 has relaxed the limitation that prevented the chef-client from running in non-disabled refresh mode.
-
The dsc_script resource may not be used in the same run-list with the dsc_resource. This is because the dsc_script resource requires that
RefreshModein the Local Configuration Manager be set toPush, whereas the dsc_resource resource requires it to be set toDisabled.NOTE: Starting with the chef-client 12.6 release, this requirement applies only for versions of Windows PowerShell earlier than 5.0.10586.0. The latest version of Windows Management Framework (WMF) 5 has relaxed the limitation that prevented the chef-client from running in non-disabled refresh mode, which allows the Local Configuration Manager to be set to
Push. -
The dsc_resource resource can only use binary- or script-based resources. Composite DSC resources may not be used.
This is because composite resources aren’t “real” resources from the perspective of the the Local Configuration Manager (LCM). Composite resources are used by the “configuration” keyword from the
PSDesiredStateConfigurationmodule, and then evaluated in that context. When using DSC to create the configuration document (the Managed Object Framework (MOF) file) from the configuration command, the composite resource is evaluated. Any individual resources from that composite resource are written into the Managed Object Framework (MOF) document. As far as the Local Configuration Manager (LCM) is concerned, there is no such thing as a composite resource. Unless that changes, the dsc_resource resource and/orInvoke-DscResourcecommand cannot directly use them.
Syntax
A dsc_resource resource block allows DSC resourcs to be used in a Chef recipe. For example, the DSC Archive resource:
Archive ExampleArchive {
Ensure = "Present"
Path = "C:\Users\Public\Documents\example.zip"
Destination = "C:\Users\Public\Documents\ExtractionPath"
} and then the same dsc_resource with Chef:
dsc_resource 'example' do resource :archive property :ensure, 'Present' property :path, "C:\Users\Public\Documents\example.zip" property :destination, "C:\Users\Public\Documents\ExtractionPath" end
The full syntax for all of the properties that are available to the dsc_resource resource is:
dsc_resource 'name' do module_name String notifies # see description property Symbol resource String subscribes # see description end
where
-
dsc_resourceis the resource -
nameis the name of the resource block -
propertyis zero (or more) properties in the DSC resource, where each property is entered on a separate line,:dsc_property_nameis the case-insensitive name of that property, and"property_value"is a Ruby value to be applied by the chef-client -
module_name,property, andresourceare properties of this resource, with the Ruby type shown. See “Properties” section below for more information about all of the properties that may be used with this resource.
Actions
This resource has the following actions:
:nothing-
Default.
Define this resource block to do nothing until notified by another resource to take action. When this resource is notified, this resource block is either run immediately or it is queued up to be run at the end of the chef-client run.
:reboot_action- Use to request an immediate reboot or to queue a reboot using the
:reboot_now(immediate reboot) or:request_reboot(queued reboot) actions built into the reboot resource.
Properties
This resource has the following properties:
ignore_failure-
Ruby Types: TrueClass, FalseClass
Continue running a recipe if a resource fails for any reason. Default value:
false. module_name-
Ruby Type: String
The name of the module from which a DSC resource originates. If this property is not specified, it will be inferred.
notifies-
Ruby Type: Symbol, ‘Chef::Resource[String]’
A resource may notify another resource to take action when its state changes. Specify a
'resource[name]', the:actionthat resource should take, and then the:timerfor that action. A resource may notifiy more than one resource; use anotifiesstatement for each resource to be notified.A timer specifies the point during the chef-client run at which a notification is run. The following timers are available:
:before- Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.
:delayed- Default. Specifies that a notification should be queued up, and then executed at the very end of the chef-client run.
-
:immediate,:immediately - Specifies that a notification should be run immediately, per resource notified.
The syntax for
notifiesis:notifies :action, 'resource[name]', :timer
property-
Ruby Type: Symbol
A property from a Desired State Configuration (DSC) resource. Use this property multiple times, one for each property in the Desired State Configuration (DSC) resource. The format for this property must follow
property :dsc_property_name, "property_value"for each DSC property added to the resource block.The
:dsc_property_namemust be a symbol.Use the following Ruby types to define
property_value:Ruby Windows PowerShell ArrayObject[]Chef::Util::Powershell:PSCredentialPSCredentialFalseClassbool($false)FixnumIntegerFloatDoubleHashHashtableTrueClassbool($true)These are converted into the corresponding Windows PowerShell type during the chef-client run.
resource-
Ruby Type: String
The name of the DSC resource. This value is case-insensitive and must be a symbol that matches the name of the DSC resource.
For built-in DSC resources, use the following values:
Value Description :archiveUse to to unpack archive (.zip) files. :environmentUse to to manage system environment variables. :fileUse to to manage files and directories. :groupUse to to manage local groups. :logUse to to log configuration messages. :packageUse to to install and manage packages. :registryUse to to manage registry keys and registry key values. :scriptUse to to run Powershell script blocks. :serviceUse to to manage services. :userUse to to manage local user accounts. :windowsfeatureUse to to add or remove Windows features and roles. :windowsoptionalfeatureUse to configure Microsoft Windows optional features. :windowsprocessUse to to configure Windows processes. Any DSC resource may be used in a Chef recipe. For example, the DSC Resource Kit contains resources for configuring Active Directory components, such as
xADDomain,xADDomainController, andxADUser. Assuming that these resources are available to the chef-client, the corresponding values for theresourceattribute would be::xADDomain,:xADDomainController, andxADUser. retries-
Ruby Type: Integer
The number of times to catch exceptions and retry the resource. Default value:
0. retry_delay-
Ruby Type: Integer
The retry delay (in seconds). Default value:
2. subscribes-
Ruby Type: Symbol, ‘Chef::Resource[String]’
A resource may listen to another resource, and then take action if the state of the resource being listened to changes. Specify a
'resource[name]', the:actionto be taken, and then the:timerfor that action.A timer specifies the point during the chef-client run at which a notification is run. The following timers are available:
:before- Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.
:delayed- Default. Specifies that a notification should be queued up, and then executed at the very end of the chef-client run.
-
:immediate,:immediately - Specifies that a notification should be run immediately, per resource notified.
The syntax for
subscribesis:subscribes :action, 'resource[name]', :timer
Examples
The following examples demonstrate various approaches for using resources in recipes. If you want to see examples of how Chef uses resources in recipes, take a closer look at the cookbooks that Chef authors and maintains: https://github.com/chef-cookbooks.
Open a Zip file
dsc_resource 'example' do resource :archive property :ensure, 'Present' property :path, 'C:\Users\Public\Documents\example.zip' property :destination, 'C:\Users\Public\Documents\ExtractionPath' end
Manage users and groups
dsc_resource 'demogroupadd' do
resource :group
property :groupname, 'demo1'
property :ensure, 'present'
end
dsc_resource 'useradd' do
resource :user
property :username, 'Foobar1'
property :fullname, 'Foobar1'
property :password, ps_credential('P@assword!')
property :ensure, 'present'
end
dsc_resource 'AddFoobar1ToUsers' do
resource :Group
property :GroupName, 'demo1'
property :MembersToInclude, ['Foobar1']
end Create a test message queue
The following example creates a file on a node (based on one that is located in a cookbook), unpacks the MessageQueue.zip Windows PowerShell module, and then uses the dsc_resource to ensure that Message Queuing (MSMQ) sub-features are installed, a test queue is created, and that permissions are set on the test queue:
cookbook_file 'cMessageQueue.zip' do
path "#{Chef::Config[:file_cache_path]}\\MessageQueue.zip"
action :create_if_missing
end
windows_zipfile "#{ENV['PROGRAMW6432']}\\WindowsPowerShell\\Modules" do
source "#{Chef::Config[:file_cache_path]}\\MessageQueue.zip"
action :unzip
end
dsc_resource 'install-sub-features' do
resource :windowsfeature
property :ensure, 'Present'
property :name, 'msmq'
property :IncludeAllSubFeature, true
end
dsc_resource 'create-test-queue' do
resource :cPrivateMsmqQueue
property :ensure, 'Present'
property :name, 'Test_Queue'
end
dsc_resource 'set-permissions' do
resource :cPrivateMsmqQueuePermissions
property :ensure, 'Present'
property :name, 'Test_Queue_Permissions'
property :QueueNames, 'Test_Queue'
property :ReadUsers, node['msmq']['read_user']
end
© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs-archive.chef.io/release/12-13/resource_dsc_resource.html