Executing chef cookbook based on dependency

94 Views Asked by At

I have two cookbooks: CookbookA and CookbookB

CookbookB has an attribute (e.g. "include_xyz") defined.
This should be set to "true" if CookbookA has run otherwise it should be set to "false".

How should "include_xyz" definition in CookbookB/attributes/default.rb look like?

2

There are 2 best solutions below

2
On

Since attribute files are all parsed (all of the attributes in all of the cookbooks which have been synchronized to the node), before any recipe code is executed, and before any include_recipe "CookbookA" statements could have been parsed this is impossible to solve accurately in attribute code.

You can eliminate the attribute entirely and just make conditional code in recipes directly:

if node.recipe?('CookbookA')
   # do stuff conditionally
end

It's probably better to invert this logic, however, and write a wrapper cookbook around CookbookA which gets the behavior correct. You're trying to 'spy' on CookbookA when it will be simpler to assert the correct behavior. You could add an attribute which drove both the behavior in your cookbook and if CookbookA was included or not, and make that attribute an authoritative source of truth.

1
On

Ahh got it!

default['include_xyz'] = "#{node.recipe?('CookbookA')}"