Check environment variable in Chef only-if guard
<p>I am trying to add a conditional guard to a Chef recipe action, so that it only runs if a certain environment variable is set (<code>CLEAN_DB</code>).</p>

<pre><code>execute "create databases" do
    Chef::Log.info("Cleaning/creating the database")
    command "psql --file #{node['sql-directory']}/sql/db_create.sql"
    cwd node['sql-directory']
    action :run
    user node['postgresql-user']
    only-if (ENV['CLEAN_DB'] == "create")
end
</code></pre>

<p>The line right before the <code>end</code> is giving me trouble.  It gives the following syntax error:</p>

<blockquote>
<pre><code>...
SyntaxError
-----------
/home/centos/.jenkins/workspace/test1__Deploy/cache/cookbooks/recipes/app_linux.rb:157: syntax error, unexpected end-of-input, expecting keyword_end
...
</code></pre>
</blockquote>

<p>I've tried all the following:</p>

<pre><code>only-if ENV['CLEAN_DB'] == "create"
only-if { ENV['CLEAN_DB'] == "create" }
only-if '("#{ENV[\'CLEAN_DB\']}" == "create")'
only-if 'if [[ "$CLEAN_DB" == "create" ]] ; then echo 0 ; else echo 1; fi;'
only-if 'if [[ "$CLEAN_DB" == "create" ]] ; then echo 0 ; else echo 1; fi'
</code></pre>

<p>And none of them seem to work.  The documentation (<a href="https://docs.chef.io/resource_common.html" rel="nofollow noreferrer">https://docs.chef.io/resource_common.html</a>) seems to suggest that all I need is a shell script that outputs 0 (in the shell case, it gives an error about a string literal in the condition) or a Ruby block (shown in the examples between <code>{}</code>). I am out of ideas at this point.</p>