简体   繁体   English

如何在厨师食谱中使用not_if

[英]How to use not_if in a chef recipe

I am new to chef so I am a little confused in how the conditional not_if works inside a execute resource. 我是厨师的新手,所以我对条件not_if如何在执行资源中工作有点困惑。 I understand that it tells chef not to execute a command if the command returns 0 or true; 据我所知,如果命令返回0或true,它告诉厨师不要执行命令; however, in my code it is apparently still running the command. 但是,在我的代码中,它显然仍在运行命令。

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资源“执行[创建用户]”

... ...

[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:执行[create-user](postgresql :: initialize第16行)出错:Mixlib :: ShellOut :: ShellCommandFailed :预期进程以[0]退出,但收到'1'

STDERR: createuser: creation of new role failed: ERROR: role "user" already exists STDERR:createuser:新角色的创建失败:ERROR:角色“user”已存在

To me it seems that it should work;however, it still running the execute. 对我来说似乎它应该工作;但是,它仍然运行执行。 Am I missing something? 我错过了什么吗?

Thank you 谢谢

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. 但是,在我的情况下,“not_if”似乎由不同的用户(root)执行,并且未能正确检查条件。 Adding [:user => "postgres"] resolved the issue. 添加[: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

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]。

First, there is a typo in the WHERE condition. 首先, WHERE条件中存在拼写错误。 It should probably be username instead of usename . 它可能应该是username而不是usename

Anyawy, you should do: Anyawy,你应该这样做:

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! 我非常爱它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM