检查 Chef 中的环境变量 only-if guard
Check environment variable in Chef only-if guard
我正在尝试向 Chef 食谱操作添加条件保护,以便它仅在设置了特定环境变量时运行 (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
之前的那一行给我带来了麻烦。它给出了以下语法错误:
...
SyntaxError
-----------
/home/centos/.jenkins/workspace/test1__Deploy/cache/cookbooks/recipes/app_linux.rb:157: syntax error, unexpected end-of-input, expecting keyword_end
...
我已经尝试了以下所有方法:
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'
其中 none 似乎有效。文档 (https://docs.chef.io/resource_common.html) 似乎表明我需要的只是一个输出 0 的 shell 脚本(在 shell 的情况下,它会给出有关条件中字符串文字的错误)或一个 Ruby 块(在 {}
之间的示例中显示)。我现在没主意了。
如果您拼写正确 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
我正在尝试向 Chef 食谱操作添加条件保护,以便它仅在设置了特定环境变量时运行 (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
之前的那一行给我带来了麻烦。它给出了以下语法错误:
... SyntaxError ----------- /home/centos/.jenkins/workspace/test1__Deploy/cache/cookbooks/recipes/app_linux.rb:157: syntax error, unexpected end-of-input, expecting keyword_end ...
我已经尝试了以下所有方法:
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'
其中 none 似乎有效。文档 (https://docs.chef.io/resource_common.html) 似乎表明我需要的只是一个输出 0 的 shell 脚本(在 shell 的情况下,它会给出有关条件中字符串文字的错误)或一个 Ruby 块(在 {}
之间的示例中显示)。我现在没主意了。
如果您拼写正确 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