[chef] Re: Re: Re: delayed evaluation?


Chronological Thread 
  • From: John Alberts < >
  • To:
  • Subject: [chef] Re: Re: Re: delayed evaluation?
  • Date: Tue, 17 Dec 2013 08:50:43 -0800

Thanks everyone for the responses.  So... it seems the consensus is that this is best done in a LWRP.  I had considered doing that, it just seemed like overkill for what should be a simple operation; that being, change the value of a variable(or attribute) and use that variable again later in the code.

@Torben, Yes, I would probably get warnings for reusing a resource if I didn't give it a unique name in the loop, but that would be the extent of the impact.

John



On Tue, Dec 17, 2013 at 12:21 AM, Torben Knerr < " target="_blank"> > wrote:

Another possible caveat:

Shouldn't it be 'ruby_block "ls /tmp #{count}" do' ?

-Torben

On Dec 17, 2013 6:39 AM, "Noah Kantrowitz" < " target="_blank"> > wrote:
The more generic solution is to use resources/LWRPs such that the second resource isn't compiled until after the settings you care about. For example https://github.com/balanced-cookbooks/ci/blob/master/libraries/ci_server.rb#L45-L46

--Noah

On Dec 16, 2013, at 9:18 PM, John Alberts < " target="_blank"> > wrote:

> This seems like such a simple problem and something that everyone should come across at one point or another.
>
> Here's a short snippet of code that illustrates the issue I'm having:
>
> (1..2).each do |count|
>   ruby_block "ls /tmp" do
>     block do
>       node.set['test1']['lsdir'] = %x[ls /tmp]
>     end
>   end
>
>   file "/tmp/file#{count}.txt" do
>     content node['test1']['lsdir']
>   end
> end
>
>
> So, at the end of the run, both /tmp/file1.txt and /tmp/file2.txt are empty.  I had hoped for /tmp/file2.txt to show that '/tmp/file1.txt now exists.
>
> I know this is because the value of attributes are evaluated during the compile phase at the beginning of the chef run and at that time, the value of node['test1']['lsdir'] is nil.  Also, this same issue arises when trying to use a variable instead of an attribute.
>
> How can I get something like this to execute as I would have hoped?  This has to be a common pattern; set an attribute, use it, modify the attribute, use the new value.  I know there is the new 'lazy' delayed evaluator, but I can only get that to work when used in a value for a resource attribute.
>
> Thanks
>
>
> --
> John Alberts




--
John Alberts



Archive powered by MHonArc 2.6.16.

§