Using Lazy Evaluation

I am attempting to to use lazy evaluation, and don’t think I quite understand how it works. I have a ruby_block that I use to set an attribute as so:

ruby_block 'get-sssd-dc' do block do Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut) adcli_info = "adcli info #{node['domain']} | grep 'domain-controller =' | awk '{print $3}'" adcli_dc_out = shell_out(adcli_info) adcli_dc = adcli_dc_out.stdout node.set['node-info']['sssd']['dc'] = adcli_dc end action :create end

I then want to use a lazy evaluation in a template erb file:

ad_server = <%= lazy {node['node-info']['sssd']['dc']} %>

This is the value that is left when it converges:

ad_server = #<Enumerator::Lazy:0x00000007a61bc8>

So the question is, is it possible to do what I am trying to do, or am I not understanding how lazy evaluation works? Thanks!

You want to use lazy in the resource definition, I believe.

template '/some/file.txt' do
  variables lazy { stuff }

Here’s a quick example with just a file resource:

lazy_value = 100

file '/tmp/lazy.txt' do
  content lazy { "#{lazy_value}" }

lazy_value = 9000
$ chef-apply lazy.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
  * file[/tmp/lazy.txt] action create
    - create new file /tmp/lazy.txt
    - update content in file /tmp/lazy.txt from none to c4fe6b
    --- /tmp/lazy.txt       2016-01-22 08:22:25.000000000 -0700
    +++ /tmp/.lazy.txt20160122-18413-48cv89 2016-01-22 08:22:25.000000000
    @@ -1 +1,2 @@

OK. That is what I was thinking. That I was using it wrong. Will have to
find another way to code this.