# Delayed evaluation?

**URL:** <https://discourse.chef.io/t/delayed-evaluation/4814>\
**Category:** Chef Infra (archive)\
**Created:** [December 17, 2013, 5:18am UTC](https://discourse.chef.io/t/delayed-evaluation/4814 "2013-12-17T05:18:30Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![Dan\_Razzell](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@Dan\_Razzell](https://discourse.chef.io/u/Dan_Razzell)\
**Post date:** [December 19, 2013, 7:25am UTC](https://discourse.chef.io/t/delayed-evaluation/4814/21 "2013-12-19T07:25:20Z")

</div>

On Wed, Dec 18, 2013 at 4:10 PM, John Alberts [john.m.alberts@gmail.com](mailto:john.m.alberts@gmail.com)wrote:

> On Wed, Dec 18, 2013 at 10:17 AM, Dan Razzell [danr@activestate.com](mailto:danr@activestate.com)wrote:
> 
> > Isn't the issue only that %x[ls /tmp] is being evaluated at compile  
> > time? Avoid that notation and you'll be able to do what you intend. Use  
> > "system" instead and it will be evaluated as part of the ruby block.
> 
> I thought any ruby code inside a ruby block will not be evaluated during  
> compile time and instead during the normal order of the execution phase. I  
> haven't tested this yet, but does it really matter if inside that ruby  
> block I've used, %x[ls /tmp] or `ls /tmp` or system("ls /tmp") ?

Testing it would be a good idea. 😉

---

<div class="post-metadata">

**Author:** ![Chris\_Roberts](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/chris_roberts/32/408_2.png) [@Chris\_Roberts](https://discourse.chef.io/u/Chris_Roberts)\
**Post date:** [December 19, 2013, 5:11pm UTC](https://discourse.chef.io/t/delayed-evaluation/4814/22 "2013-12-19T17:11:22Z")

</div>

Hi. You probably want:

content lazy{ do some stuff and return string }  
On Dec 16, 2013 9:19 PM, "John Alberts" [john.m.alberts@gmail.com](mailto:john.m.alberts@gmail.com) 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

---

<div class="post-metadata">

**Author:** ![John\_Alberts](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/john_alberts/32/293_2.png) [@John\_Alberts](https://discourse.chef.io/u/John_Alberts)\
**Post date:** [December 19, 2013, 5:15pm UTC](https://discourse.chef.io/t/delayed-evaluation/4814/23 "2013-12-19T17:15:01Z")

</div>

On Thu, Dec 19, 2013 at 9:11 AM, Chris Roberts  
[chrisroberts.code@gmail.com](mailto:chrisroberts.code@gmail.com)wrote:

> Hi. You probably want:
> 
> content lazy{ do some stuff and return string }

I think I tried that but I I guess I didn't get the syntax correct. Every  
variation I tried kept returning errors, so I assumed I was using 'lazy' in  
a place where it shouldn't be. I'll give this a try again.

Thanks

--  
John Alberts

---

<div class="post-metadata">

**Author:** ![Chris\_Roberts](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/chris_roberts/32/408_2.png) [@Chris\_Roberts](https://discourse.chef.io/u/Chris_Roberts)\
**Post date:** [December 19, 2013, 5:22pm UTC](https://discourse.chef.io/t/delayed-evaluation/4814/24 "2013-12-19T17:22:11Z")

</div>

It was introduced in 11.6 and 10.20-something. And the delayed evaluator  
cookbook will back port it to earlier 11 versions (and maybe 10 too. I  
can't remember off the top of my head)  
On Dec 19, 2013 9:15 AM, "John Alberts" [john.m.alberts@gmail.com](mailto:john.m.alberts@gmail.com) wrote:

> On Thu, Dec 19, 2013 at 9:11 AM, Chris Roberts \<  
> [chrisroberts.code@gmail.com](mailto:chrisroberts.code@gmail.com)\> wrote:
> 
> > Hi. You probably want:
> > 
> > content lazy{ do some stuff and return string }
> 
> I think I tried that but I I guess I didn't get the syntax correct. Every  
> variation I tried kept returning errors, so I assumed I was using 'lazy' in  
> a place where it shouldn't be. I'll give this a try again.
> 
> Thanks
> 
> --  
> John Alberts

[Previous page](https://discourse.chef.io/t/delayed-evaluation/4814.md?page=1)
