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?

- 944
- 3
- 12
- 28
2 Answers
Ahh got it!
default['include_xyz'] = "#{node.recipe?('CookbookA')}"

- 944
- 3
- 12
- 28
-
2That isn't going to work the way that you think it does, and I can't think of a good reason to want this kind of attribute. You're better off not even trying to put this kind of a boolean variable into an attribute and just using that statement directly in your recipe code. – lamont Jun 13 '17 at 18:28
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.

- 3,854
- 1
- 20
- 26
-
Thanks lamont for your reply. My usecase is slightly different. Here CookbookA provides FeatureA. CookbookB will update a props file depending on weather FeatureA is enabled or not (=true if CookbookA is run. =false otherwise). CookbookB has a resource which starts a service which checks for this prop value and do things differently based on the setting. – Haris Farooqui Jun 13 '17 at 18:50
-
I understood what you were trying to do. You cannot reliably do that, and you should have a higher level abstraction which is responsible for applying Cookbook A and Cookbook B together. The way that you're thinking about solving this problem ultimately won't work. – lamont Jun 16 '17 at 02:10