Understanding Chef only_if not_if

a.m. picture a.m. · Dec 5, 2011 · Viewed 30.1k times · Source

Im not sure I understand Chef conditional execution.

I'd like to do some conditional execution based on whether or not a database exists in Postgresql

So here's my example

execute "add_db" do
  cwd "/tmp"
  user "dbuser"
  command "createdb -T template_postgis mydb"
  not_if 'psql --list|grep mydb'
end

Running psql --list|grep mydb return what you would expect if the db exists (the line with the dbname entry) and nothing at all if it doesnt.

So how does not_if only evaluate that? True or false? 1 or 0? Dont all processes return 0 if they are successful?

Anywho any advice would be greatly appreciated!

Answer

Darrin Holst picture Darrin Holst · Jul 8, 2012

I just ran into this issue. My problem was that the not_if command was being run as 'root', not 'dbuser'. If you change it to

not_if 'psql --list|grep mydb', :user => 'dbuser'

then you may get the results you were looking for.

http://tickets.opscode.com/browse/CHEF-438