0

这是我action :create的自定义 LWRP,但它始终运行。我想做一个幂等动作。

这是我的实现:

action :create do
  converge_by "Create [#{@new_resource}]" do

    USER_NAME     = @new_resource.username
    USER_PASSWORD = @new_resource.password unless (@new_resource.password.nil? ||
                                                        @new_resource.password.empty?)
    USER_PASSWORD = USER_NAME
    DATABASE_NAME = @new_resource.database_name unless (@new_resource.database_name.nil? ||
                                                              @new_resource.database_name.empty?)
    DATABASE_NAME = USER_NAME

    execute "create user [#{USER_NAME}]" do
      user "postgres"
      exists = <<-EOH
      PGPASSWORD='postgres' psql -U postgres -c "select * from pg_user where usename='#{USER_NAME}'" | grep -c #{USER_NAME}
      EOH
      command "PGPASSWORD='postgres' createuser -U postgres -sw #{USER_NAME}"
      not_if exists, :user => "postgres"
    end

    execute "set-password" do
      user "postgres"
      command "PGPASSWORD='postgres' psql -U postgres -c \"alter role #{USER_NAME} with password '#{USER_PASSWORD}'\""
    end

    execute "create-database" do
      user "postgres"
      exists = <<-EOH
      PGPASSWORD='postgres' psql -U postgres -c "select * from pg_database WHERE datname='#{DATABASE_NAME}'" | grep -c #{DATABASE_NAME}
      EOH
      command "PGPASSWORD='postgres' createdb -U postgres -O #{USER_NAME} -T template0 #{DATABASE_NAME}"
      not_if exists, :user => "postgres"
    end
  end
  @new_resource.updated_by_last_action(true)
end

为什么最后一行不起作用@new_resource.updated_by_last_action(true)

4

1 回答 1

0

虽然我没有直接回答您的问题,但您可能对 ToASTER 感兴趣,这是一个用于测试 Chef 脚本幂等性的框架。

http://cloud-toaster.github.io/

Chef 配方在隔离的容器环境 (Docker VM) 中以不同的配置执行,ToASTER 报告各种指标,例如系统状态变化、收敛属性和幂等性问题。

于 2014-06-19T11:33:43.347 回答