spuder
spuder

Reputation: 18427

Can CHEF lazy load multiple attributes?

If you have a wrapper cookbook that overrides a derived attributes, you need to use lazy loading like so:

Library cookbook

default['foo'] = 42
default['url'] = "http://example.com/#{foo}"

Wrapper cookbook

default['foo'] = 9000
default['url'] = "http://example.com/#{foo}"

some_resource 'derp' do
  url node['url'] % { foo: node['foo'] }
  action :do_stuff
end

The above works.

How do you use lazy loading for multiple derived attributes?

For example, I need to derive url off of derp which is derived off of foo

 default['foo'] = 42
 default['derp'] = "bar-#{foo}"
 default['url'] = "http://example.com/#{derp}"

some_resource 'derp' do
  url node['url'] % { foo: node['foo'], derp: node['derp'] } #Guessing this is the right syntax
  action :do_stuff
end

However this gives the error

       KeyError
   --------
   key{node['url']} not found

This example is greatly simplified, the full code is outlined here: https://github.com/SimpleFinance/chef-zookeeper/issues/151

Update

For reference, the issue was resolved here: https://github.com/SimpleFinance/chef-zookeeper/commit/6750ea8c11a6dd7ef1c0f76ac8c61b71a172fb80

Upvotes: 1

Views: 480

Answers (1)

coderanger
coderanger

Reputation: 54211

As I've explained to you before, this works like sprintf in any normal C-based language. There is no magic. You are also still using #{} in the string instead of %{} so none of this will work.

Please follow the examples I've given you:

default['foo'] = 42  
default['derp'] = "bar-%{foo}"  
default['url'] = "http://example.com/%{derp}"  

some_resource 'derp' do
  derp = node['derp'] % {foo: node['foo']}
  url node['url'] % { foo: node['foo'], derp: derp } 
end

Upvotes: 1

Related Questions