postgresql 如何在厨师食谱中使用 not_if
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14530784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use not_if in a chef recipe
提问by svs
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.
我是厨师的新手,所以我对条件 not_if 在执行资源中的工作方式感到有些困惑。我知道如果命令返回 0 或 true,它会告诉厨师不要执行命令;但是,在我的代码中,它显然仍在运行该命令。
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:
以下代码应该创建一个用户(及其密码)和一个数据库;但是,如果用户和数据库已经存在,则不应执行任何操作。用户、数据库和密码在属性中定义。以下是我的代码:
execute "create-user" do
code = <<-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 = <<-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
Chef gives me the following error:
厨师给了我以下错误:
Error executing action run
on resource 'execute[create-user]'
run
对资源“execute[create-user]”执行操作时出错
...
...
[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'
[2013-01-25T12:24:51-08:00] 致命:Mixlib::ShellOut::ShellCommandFailed:execute[create-user](postgresql::initialize line 16)有一个错误:Mixlib::ShellOut::ShellCommandFailed : 预期进程以 [0] 退出,但收到“1”
STDERR: createuser: creation of new role failed: ERROR: role "user" already exists
STDERR:createuser:创建新角色失败:错误:角色“用户”已经存在
To me it seems that it should work;however, it still running the execute. Am I missing something?
对我来说,它似乎应该可以工作;但是,它仍在运行执行。我错过了什么吗?
Thank you
谢谢
回答by parroty
I had been having the same issue. But, in my case, "not_if" seems executed by different user (root), and failed to check the condition properly. Adding [:user => "postgres"] resolved the issue.
我一直有同样的问题。但是,就我而言,“not_if”似乎由不同的用户(root)执行,并且未能正确检查条件。添加 [:user => "postgres"] 解决了这个问题。
execute "create-database-user" do
user "postgres"
exists = <<-EOH
psql -U postgres -c "select * from pg_user where usename='#{settings[:username]}'" | grep -c #{settings[:username]}
EOH
command "createuser -U postgres -sw #{settings[:username]}"
not_if exists, :user => "postgres"
end
I've referred the following code example.
我参考了以下代码示例。
https://github.com/MarcinKoziuk/chef-postgres-dbsetup/blob/master/recipes/default.rb
https://github.com/MarcinKoziuk/chef-postgres-dbsetup/blob/master/recipes/default.rb
回答by Christophe Biocca
You're checking for the existence of:
您正在检查是否存在:
node[:user]
If it doesn't exist, you create:
如果它不存在,则创建:
node[:postgresql][:user]
Unless these happen to be equal, you'll keep trying to create node[:postgresql][:user] repeatedly.
除非这些碰巧相等,否则您将继续尝试重复创建 node[:postgresql][:user]。
回答by Eye
First, there is a typo in the WHERE
condition. It should probably be username
instead of usename
.
首先,条件中有一个错字WHERE
。它可能应该username
代替usename
.
Anyawy, you should do:
无论如何,你应该这样做:
execute "create-user" do
user "postgres"
command "createuser -s #{node[:user]}"
not_if "psql -U postgres -c \"select * from pg_user where username='#{node[:user]}'\" | grep -c #{node[:user]}"
end
This assumes that your psql -U postgres -c "select * from pg_user where username='#{node[:user]}'"
is correct.
这假设您psql -U postgres -c "select * from pg_user where username='#{node[:user]}'"
是正确的。
Same with a database:
与数据库相同:
execute "create-database" do
user "postgres"
command "createdb #{node[:database]}"
not_if "psql -U postgres -c \"select * from pg_database WHERE datname='#{node[:database]}'\" | grep -c #{node[:database]}}"
end
Regarding the username, even if it already exists, changing the password to the known one shouldn't cause a problem. After all you know the password.
关于用户名,即使它已经存在,将密码更改为已知的也不应该造成问题。毕竟你知道密码。
FYI, you can define multiple conditionals within one resource.
仅供参考,您可以在一个资源中定义多个条件。
Good luck with Chef! I love it very much!
祝大厨好运!我非常爱它!