title: How to use not_if in a chef recipe
body: <p>I am new to chef so I am a little confused in how the conditional not_if works inside a execute resource. I understand that it tells chef not to execute a command if the command returns 0 or true; however, in my code it is apparently still running the command.</p>
  
  <p>The following code is supposed to create a user (and its password) and a database; however, if the user and database already exist, it should not do anything. The user, database and password are defined in the attributes. The following is the code I have:</p>
  
  <pre><code>execute "create-user" do
  
          code = &lt;&lt;-EOH
          psql -U postgres -c "select * from pg_user where usename='#{node[:user]}'" | grep -c #{node[:user]}
          EOH
          user "postgres"
          command "createuser -s #{node[:user]}"
          not_if code
  end
  
  
  execute "set-user-password" do
      user "postgres"
      command  "psql -U postgres -d template1 -c \"ALTER USER #{node[:user]} WITH PASSWORD '#{node[:password]}';\""
  end
  
  
  execute "create-database" do
      exists = &lt;&lt;-EOH
      psql -U postgres -c "select * from pg_database WHERE datname='#{node[:database]}'" | grep -c #{node[:database]}}
      EOH
      user "postgres"
      command "createdb #{node[:database]}"
     not_if exists
  
  end
  </code></pre>
  
  <p>Chef gives me the following error:</p>
  
  <p>Error executing action <code>run</code> on resource 'execute[create-user]'</p>
  
  <p>...</p>
  
  <p>[2013-01-25T12:24:51-08:00] FATAL: Mixlib::ShellOut::ShellCommandFailed: execute[create-user] (postgresql::initialize line 16) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'</p>
  
  <p>STDERR: createuser: creation of new role failed: ERROR:  role "user" already exists</p>
  
  <p>To me it seems that it should work;however, it still running the execute. Am I missing something?</p>
  
  <p>Thank you</p>