Is it possible to override attributes for dsc_resource for environment on chef server?


#1

I have DSC Resource defined as below in runlist

dsc_resource 'Change SN header' do
resource :ConfigureSNheader
property :FullName, "Yes"
property :Ensure, "Present"
module_name :MyDSCResource
end

I’m trying to figure out if I can override attribute property FullName of this resource in Environment on chef server. I tried below which did not work. Confused what would be the proper syntax or if it’s possible in general.

{
  "dsc_resource": {
    "property": {
      "FullName": "No"
    }
  }
}

#2

Sure, you could do something like

dsc_resource 'Change SN header' do
  resource :ConfigureSNheader
  property :FullName, node["mycookbook"]["ConfigureSNheader"]["fullname"]
  property :Ensure, "Present"
  module_name :MyDSCResource
end

Your attribute in the environment would then look something like

{
  "mycookbook": {
    "ConfigureSNheader": {
      "fullname": "No"
    }
}

The attribute names I picked are not related to the resource name or properties by any magic, it’s just how I would access them off the node object. The convention would be that attributes are namespaced off the cookbook in which they were introduced. You could then include your default value for the attribute in an attributes file in your cookbook.

You can find more about attributes in the Chef docs and this great video


#3

Thanks,

What would be the best practices setup for such cases. While I was waiting how to do it via chef server attribute overwrite I found a workaround to use conditional statement in recipe instead. Not sure if it’s a good idea to do this way or perform attribute override on chef server.

if node.chef_environment == "dev" 
RubyFullName = "Yes";
else
RubyFullName = "No";
end


dsc_resource 'Change SN header' do
resource :ConfigureSNheader
property :FullName, RubyFullName
property :Ensure, "Present"
module_name :MyDSCResource
end

#4

I lean towards providing that type of data in an attribute rather than putting explicit knowledge of my environment into the recipe. If you do put the logic to determine environment in the recipe, you’ll have to go and change the recipe any time you need to change environment names, add other conditional logic, etc…

In this case, it’s pretty straight forward. You have two values and either you are in the “dev” environment or not. Other scenarios where you may want to vary data across environments or other conditionals may have more permutations and your recipes become much more complex. I’d strive for as much simplicity in your recipe as possible to make it easier to understand when you come back to it 6 weeks or 6 months later. Keeping your pattern of how you access variable data consistent also makes it easier for you to maintain multiple cookbooks and recipes going forward.