[chef] Re: Re: Wrapper cookbooks, templates, lazy


Chronological Thread 
  • From: Justin Dossey < >
  • To:
  • Subject: [chef] Re: Re: Wrapper cookbooks, templates, lazy
  • Date: Thu, 16 Oct 2014 16:17:36 -0700

This excellent nugget of information led me to create a nifty little recipe showing the agony we put Chef through in our daily lives.

Here's my recipe, bar::default (gist at https://gist.github.com/justindossey/7313509e36c1b271ef24 )

node.default.bar.tags = ['tag1-bar']

file "/tmp/bar.txt" do
  content lazy { node.bar.tags.join(',') }
end

ruby_block 'update second tag' do
  block { node.default.bar.tags << 'tag2-bar' }
  only_if { node.default.bar.tags << 'tag3-bar' }
  notifies :create, 'file[/tmp/bar.txt]', :delayed
end

r = resources(file: '/tmp/bar.txt')
r.content(Chef::DelayedEvaluator.new {
    node.default.bar.tags << 'tag4-bar'
    node.bar.tags.join(',')
})


Quickly: what will be written to my file /tmp/bar.txt?

kitchen converge output at https://gist.github.com/justindossey/ecbcaa1a90d940ce2acc

I wasn't sure what I was going to get-- that was the point of the experiment-- but I was certainly surprised to get what I did!

On Thu, Oct 16, 2014 at 11:11 AM, Noah Kantrowitz < " target="_blank"> > wrote:

On Oct 16, 2014, at 10:49 AM, Justin Dossey < "> > wrote:

> I ran into an interesting one today.
>
> If you're wrapping a (community) cookbook and your wrapper needs lazy {} to move evaluation of something to the execute phase, the #resources method doesn't work.
>
> Example
>
> Cookbook foo, recipes/default.rb
>
> file '/tmp/foo.txt' do
>    owner node['foo']['user']
> end
>
> Cookbook bar, recipes/default.rb
> include_recipe 'foo::default'
>
> user 'new_user'
> node.override['foo']['user'] = 'new_user'
>
> # this doesn't work:
> resources(file: '/tmp/foo.txt').owner( lazy { node['foo']['user'] })

Use this:

resources(file: '/tmp/foo.txt').owner(Chef::DelayedEvaluator.new { node['foo']['user'] })

#lazy is just a helper method for that, but only available in the resource class scope.

--Noah




--
Justin Dossey
Practice Owner
New Context Services, Inc



Archive powered by MHonArc 2.6.16.

§