title: Chef Recipes - Setting node attributes in ruby_block
body: <p>I have a Chef recipe for a multi-node web service, each node of which needs to get the hostname and IP of the other nodes, to put it into its own local configuration.</p>
  
  <p>The code is shown below. The problem is that when the node.set[][] assignments are made in the ruby_block as shown, the values are empty when the template that relies upon them is created. If I want to create that template, I have to move all of the ruby_block code outside, and have it "loose" in the recipe. Which makes it harder to do unit-testing with Chefspec and the like.</p>
  
  <p>Can any Chef guru set me straight? Is it just impossible to do node.set[] like this inside of a ruby_block? And if so, why doesn't it say so in the <a href="http://docs.opscode.com/resource_ruby_block.html" rel="noreferrer">docs?</a></p>
  
  <pre><code>$cm = { :name =&gt; "web", :hostname =&gt; "" , :ip_addr =&gt; "" }
  $ca = { :name =&gt; "data", :hostname =&gt; "" , :ip_addr =&gt; "" }
  $cg = { :name =&gt; "gateway", :hostname =&gt; "" , :ip_addr =&gt; "" }
  $component_list = [$cm, $ca, $cg]
  
  ruby_block "get host addresses" do
    block do
      for cmpnt in $component_list
         # do REST calls to external service to get cmpnt.hostname, ip_addr
         # .......
         node.set[cmpnt.name]['name'] = cmpnt.name
         node.set[cmpnt.name]['host'] = cmpnt.hostname
         node.set[cmpnt.name]['ip'] = cmpnt.ip_addr   
      end
    end
  end
  
  template "/etc/app/configuration/config.xml" do
    source "config.xml.erb"
    variables( :dataHost =&gt; node['data']['host'],
         :webHost =&gt;  node['web']['host'],
         :gatewayHost =&gt;  node['gateway']['host'] )
    action :create
  end
  </code></pre>
  
  <p>I also added </p>
  
  <pre><code>  subscribes  :create, "ruby_block[get host addresses]", :immediately
  </code></pre>
  
  <p>to the template definition to ensure that the ruby_block ran before the template was created. This didn't make a difference.</p>