Overriding computed attributes

Ohai,

Assume we have an attribute file in cookbook A:

default[:foo][:ver] = '1.0’
default[:foo][:url] = “http://…#{node[:foo][:ver]}”

Then in cookbook B:

override[:foo][:ver] = ‘2.0’

Will node[:foo][:url] be the expected value?

I’m not seeing this, so was wondering if there’s a way to achieve it…

/Jeppe

Jeppe:

No, it will not. Because of how the attributes are evaluated, the defaults will be set, then node[:foo][:ver] will be overridden. It’s generally considered best practice, if you’re going to use attributes to compute another attribute, to set the computed attribute in a recipe. Here’s a nice resource for this (and other tidbits): https://wiki.opscode.com/display/chef/Cookbook+Style+Guide+Draft#CookbookStyleGuideDraft-GeneratingData


Jeff Byrnes
@berkleebassist
Operations Engineer
EverTrue
704.516.4628

On May 19, 2014 at 1:42:32 PM, Jeppe Nejsum Madsen (jeppe@ingolfs.dk) wrote:

Ohai,

Assume we have an attribute file in cookbook A:

default[:foo][:ver] = '1.0’
default[:foo][:url] = “http://…#{node[:foo][:ver]}”

Then in cookbook B:

override[:foo][:ver] = ‘2.0’

Will node[:foo][:url] be the expected value?

I’m not seeing this, so was wondering if there’s a way to achieve it…

/Jeppe

On Mon May 19 10:45:22 2014, Jeff Byrnes wrote:

Jeppe:

No, it will not. Because of how the attributes are evaluated, the
defaults will be set, then node[:foo][:ver] will be overridden. It’s
generally considered best practice, if you’re going to use attributes
to compute another attribute, to set the computed attribute in a
recipe. Here’s a nice resource for this (and other tidbits):
https://wiki.opscode.com/display/chef/Cookbook+Style+Guide+Draft#CookbookStyleGuideDraft-GeneratingData

That is way out of date.

Generally setting attributes in recipes leads to compile/converge mode
sadness, and setting attributes in recipes should be avoided
(ultimately down this road you wind up finding out why force_default
and force_override got added to Chef, but you need a few layers of
wrapping cookbooks to get there).

Using roles gets overriding computed attributes right.

Otherwise, set both of the attributes explicitly in your role cookbook.

PolicyFile in Chef 12 should help sort this out.

Ok, we do have a role we could use here, so I'll try that.

Thanks

/Jeppe

On Mon, May 19, 2014 at 9:21 PM, Lamont Granquist lamont@opscode.comwrote:

On Mon May 19 10:45:22 2014, Jeff Byrnes wrote:

Jeppe:

No, it will not. Because of how the attributes are evaluated, the
defaults will be set, then node[:foo][:ver] will be overridden. It’s
generally considered best practice, if you’re going to use attributes
to compute another attribute, to set the computed attribute in a
recipe. Here’s a nice resource for this (and other tidbits):
https://wiki.opscode.com/display/chef/Cookbook+Style+Guide+Draft#
CookbookStyleGuideDraft-GeneratingData

That is way out of date.

Generally setting attributes in recipes leads to compile/converge mode
sadness, and setting attributes in recipes should be avoided (ultimately
down this road you wind up finding out why force_default and force_override
got added to Chef, but you need a few layers of wrapping cookbooks to get
there).

Using roles gets overriding computed attributes right.

Otherwise, set both of the attributes explicitly in your role cookbook.

PolicyFile in Chef 12 should help sort this out.