title: Passing variables between chef resources
body: <p>i would like to show you my use case and then discuss possible solutions:</p>
  
  <p>Problem A:
  i have 2 recipes, "a" and "b".. "a" installs some program on my file system (say at "/usr/local/bin/stuff.sh" and recipe "b" needs to run this and do something with the output.</p>
  
  <p>so recipe "a" looks something like:</p>
  
  <pre><code>execute "echo 'echo stuff' &gt; /usr/local/bin/stuff.sh" 
  </code></pre>
  
  <p>(the script just echo(es) "stuff" to stdout)</p>
  
  <p>and recipe "b" looks something like:</p>
  
  <pre><code>include_recipe "a"
  var=`/usr/local/bin/stuff.sh` 
  </code></pre>
  
  <p>(note the backquotes, <code>var</code> should contain <code>stuff</code>)</p>
  
  <p>and now i need to do something with it, for instance create a user with this username. so at script "b" i add</p>
  
  <pre><code>user "#{node[:var]}"
  </code></pre>
  
  <p>As it happens, this doesn't work.. apparently chef runs everything that is not a resource and only then runs the resources so as soon as i run the script chef complains that it cannot compile because it first tries to run the "var=..." line at recipe "b" and fails because the "execute ..." at recipe a did not run yet and so the "stuff.sh" script does not exist yet.
  Needless to say, this is extremely annoying as it breaks the "Chef runs everything in order from top to bottom" that i was promised when i started using it.
  However, i am not very picky so i started looking for alternative solutions to this problem, so:</p>
  
  <p>Problem B: i've run across the idea of "ruby_block". apparently, this is a resource so it will be evaluated along with the other resources. I said ok, then i'd like to create the script, get the output in a "ruby_block" and then pass it to "user". so recipe "b" now looks something like:</p>
  
  <pre><code>include_recipe "a"
  
  ruby_block "a_block" do
    block do
      node.default[:var] = `/usr/local/bin/stuff.sh`
    end
  end
  
  user "#{node[:var]}"
  </code></pre>
  
  <p>However, as it turns out the variable (<code>var</code>) was not passed from "ruby_block" to "user" and it remains empty. No matter what juggling i've tried to do with it i failed (or maybe i just didn't find the correct juggling method)</p>
  
  <p>To the chef/ruby masters around: How do i solve Problem A? How do i solve Problem B?</p>