我正在尝试将条件保护添加到Chef配方操作,以便只有在设置了某个环境变量(picker
)时它才会运行。
gotoDate
CLEAN_DB
之前的一行给我带来了麻烦。它给出以下语法错误:
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
我已经尝试了以下所有方法:
end
它们似乎都不起作用。文档(https://docs.chef.io/resource_common.html)似乎表明我需要的是一个输出0的shell脚本(在shell的情况下,它给出了条件中字符串文字的错误)或Ruby块(如示例所示)在...
SyntaxError
-----------
/home/centos/.jenkins/workspace/test1__Deploy/cache/cookbooks/recipes/app_linux.rb:157: syntax error, unexpected end-of-input, expecting keyword_end
...
之间。我现在没有想法。
答案 0 :(得分:3)
如果你能正确拼写它only_if
,那么你至少应该在第二次尝试(最后一个块)时成功。
正确的语法是:
execute "create databases" do
# Logging here makes IIRC no sense because it's emitted at compile time
# 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
或者,您可以使用
only_if do ENV['CLEAN_DB'] == "create" end