Chef cookbooks asking for windows and Mac dependencies for Linux box

233 views Asked by At

I assume this is something really stupid I'm not doing but can't seem to find the answer.

I'm using chef with the default precise32 vagrant box

EDIT: To clarify, I'm asking is there somewhere I can set the platform so that cookbooks and make dependency decisions.

Thanks

2

There are 2 answers

2
StephenKing On BEST ANSWER

Chef does not allow attribute-dependent cookbook dependencies (currently with chef 11). The cookbooks will be loaded, but not executed. IIRC support for that was on the wish list for chef 12.

0
sethvargo On

Stephen's answer is correct, but there are some ways to get around this.

In many community cookbooks, you'll see this pattern:

default['cookbook']['attribute'] = case node['platform_family']
                                   when 'debian'
                                     'foo'
                                   when 'rhel'
                                     'bar'
                                   end

But this quickly becomes unmanageable and the logic is difficult to follow. Additionally, adding support for other platforms becomes cumbersome.

Another option is the "one platform per attribute file" pattern. It is important to note, as Stephen said, each file is loaded, regardless of platform, so the naming conventions are actually irrelevant of their content; it's more for humans than Chef.

cookbook
  |_ attributes
    |_ mac_os_x.rb
    |_ ubuntu.rb
    |_ <platform>.rb

And at the very top of each attribute file, add something like this:

return unless platform_family?('<platform>')

This will prevent Chef from reading the rest of that file. The end result is that it "appears" Chef is supporting varying attributes by platform, but you are really just using Ruby.