厨师食谱 - not_if与SQL查询结果

时间:2017-01-31 18:29:11

标签: ruby chef

我的食谱中有这个块:

bash "Create admin database tables" do
    code "mysql -u root -D admindb < /vagrant/files/admin.sql"
    not_if shell_out("mysql -u root -s --skip-column-names -e 'SELECT COUNT(DISTINCT table_name)>0 FROM information_schema.columns WHERE table_schema = \"admindb\";'").stdout().chomp()
end

选择查询的结果是01写入stdout(仅此而已)。如果查询结果为0,我只想运行shell命令。我怎样才能实现它?

1 个答案:

答案 0 :(得分:2)

您只需要在块内使用Ruby相等运算符。既然你说“只有”输出是0,那么使用only_if而不是not_if更简单:

only_if { shell_out("mysql -u root -s --skip-column-names -e 'SELECT COUNT(DISTINCT table_name)>0 FROM information_schema.columns WHERE table_schema = \"admindb\";'").stdout.chomp == '0' }