title: Chef - create template with dynamic variable?
body: <p>I'm having a bit of a challenge on a Chef recipe. I'm new to Chef, so please bear with me.</p>
  
  <p>Step 1: My chef recipe installs Ruby Passenger, then compiles the Passenger nginx module along with Nginx.</p>
  
  <pre><code># Install passenger and nginx module
  bash "Install Passenger" do
    code &lt;&lt;-EOF
    source /usr/local/rvm/scripts/rvm
    gem install passenger
    EOF
    user "root"
    not_if { `gem list`.lines.grep(/^passenger \(.*\)/).count &gt; 0 }
  end
  
  # Install passenger
  # Note that we have to explicitly include the RVM script otherwise it won't setup the environment correctly
  bash "Install passenger nginx module and nginx from source" do
    code &lt;&lt;-EOF
    source /usr/local/rvm/scripts/rvm
    passenger-install-nginx-module --auto --prefix=/opt/nginx --auto-download
    EOF
    user "root"
    not_if { File.directory? "/opt/nginx" }
  end
  </code></pre>
  
  <p>Step 2: After that, I create the nginx config file using a template. This configuration requires the location of Passenger, which is dependent on step 1 completing.</p>
  
  <pre><code>template "/opt/nginx/conf/nginx.conf" do
    source "nginx.conf.erb"
    action :create
    variables(
      deploy_user: deploy_user,
      passenger_root: `bash -c "source /usr/local/rvm/scripts/rvm; passenger-config --root"`.chomp,
      passenger_ruby: `bash -c "source /usr/local/rvm/scripts/rvm; which ruby"`.chomp,
      passenger: node[:passenger]
    )
  end
  </code></pre>
  
  <p>Problem: Chef appears to compile templates at th ebeginning of the run. So what ends up happening is that Step 2 is actually compiled before Step 1 is run. This means that the passenger_root variable is blank. It needs Step 1 to complete before being able to get the passenger_root, then run the template.</p>
  
  <p>I tried wrapping the step 2 code in a <code>ruby_block</code> but that doesn't work: <code>undefined method</code>template' for Chef::Resource::RubyBlock`.</p>
  
  <p>Not sure what to do here, or what is the best practice for Chef for something like this?</p>
  
  <p>Thanks in advance,</p>
  
  <p>Leonard</p>