Hi,
I have environmental wrapper cookbooks using attributes that are mainly static but quite a few derived.
90% of those attributes are resolved correctly but some of the derived are returning blank even though I can trace them back logically.
Anyone know the order attributes are loaded prior to the run?
I've seen documentation on the chef site [About Attributes](http://About Attributes) but all mine are default
e.g.
File : attributes \ _enviroment.rb
default['corp_app'']['environment']['server_path'] = "E:\Apps\Corp_App"
File : attributes \ appfile.rb
default['corp_app']['environment']['TempFilesLocation'] = "#{node['corp_app'']['environment']['server_path']}\Files\Temp"
Are they loaded alphabetically, filenames first, then internal attribute name?
sorry for basic question long time since I've troubleshooted this ...
Cheers
David
Yes, attribute values are whatever is set last (at the same precedence level) - âlast-in wins", and attribute files are evaluated in a-z order.
However, derived attributes donât work like anyone expects. They work like environment variables, not like derived variables in a programming languages. So with derived attributes, the value is plugged in with whatever it is set at the time the line is read, not after all the attribute files are read. This is likely the scenario you are hitting in your code.
If you have a OâReilly Safari subscription, Iâd recommend checking out the âManaging Settings With Chefâ chapter in my Learning Chef video. In particular the âComposed Attributes are Badâ section - https://learning.oreilly.com/videos/learning-chef-for/9781491959442/9781491959442-video303723 (What you call âderived attributesâ).
Hereâs the exact example from the video:
# attributes/default.rb
default[âtomcat_homeâ] = â/usr/local/tomcat6'
default[âtomcat_binâ] = "#{node[âtomcat_homeâ]/binâ]
# attributes/tomcat.rb
default[âtomcat_homeâ] = â/usr/local/tomcat8'
Youâll discover that node[âtomcat_homeâ]
is /usr/local/tomcat6/bin
not /usr/local/tomcat8/bin
like youâd expect.
Chef will process default.rb
first, then tomcat.rb
, like youâd expect.
However, any variable replacements happen with the value when it is read. So unfortunately, like in this case - node[âtomcat_binâ]
is set to whatever tomcat_home
is set to when the line is being read. Since Chef hasnât read attributes/tomcat.rb
- itâs whatever tomcat_home
is currently set to, like an environment variable.
There are a variety of approaches to solve this issue. There are basically two camps - one camp avoids interpolated values completely and sets the entire attribute as a single string to make things more clear to people who donât know all the ins and outs of how Chef attributes work. Others will use ruby syntax tricks like using %{}
to perform delayed interpolation. You can decide which approach works best for you.
1 Like
@misheska Great detailed response much appreciated - will work through it and hopefully resolved my problem