厨师模板通过args覆盖变量

时间:2013-04-17 13:42:07

标签: automation chef chef-recipe chef-solo

我是chef-solo opscode的新手,并没有找到通往以下的方式

我正在编写一个模板并将data_bag json作为变量传递给它

- 食谱

HOME = ""

app_config = data_bag_item("config","app")

template "#{HOME}/app/config/database.yml" do
  local true
  source "#{HOME}/app/config/database.yml.erb"
  variables app_config["database.yml"]
endt

- erb

development:
  adapter: <%= @development["adapter"] %>
  database: <%= @development["database"] %>
  username: <%= @development["username"] %>
  password: <%= @development["password"] %>
  encoding: <%= @development["encoding"] %>
  host: <%= @development["host"] %>

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: <%= @test["adapter"] %>
  database: <%= @test["database"] %>
  username: <%= @test["username"] %>
  password: <%= @test["password"] %>
  encoding: <%= @test["encoding"] %>
  host: <%= @test["host"] %>

production:
  adapter: <%= @production["adapter"] %>
  database: <%= @production["database"] %>
  username: <%= @production["username"] %>
  password: <%= @production["password"] %>
  encoding: <%= @production["encoding"] %>
  host: <%= @production["host"] %>

- 数据包

{
  "id" : "app",

   "database.yml" : {
    "development": {
      "adapter"   : "mysql2",
      "database"  : "app_site",
      "username"  : "root",
      "password"  : "",
      "encoding"  : "utf8",
      "host"      : "localhost"
    } ,
    "production" : {
      "adapter"   : "mysql2",
      "database"  : "app_site",
      "username"  : "root",
      "password"  : "",
      "encoding"  : "utf8",
      "host"      : "localhost"
    } ,
    "test": {
      "adapter"   : "mysql2",
      "database"  : "app_site_test",
      "username"  : "root",
      "password"  : "",
      "encoding"  : "utf8",
      "host"      : "localhost"
    }
  },

  "config.yml":{
     "development":{},
     "production" :{},
     "test":{}
  }
}

一切运作良好,我想要进一步的是,当我执行下面的

sudo chef-solo -c solo.rb -j solo.json @development["password"]=my_new_passwd

@development["password"]应该覆盖我传递的新值而不是数据包

ANy提示?

或者有关合并两个数据包的想法?

编辑:

我想将http://apidock.com/rails/Hash/deep_merge添加到chef init的某个地方,所以Hash类会有deep_merge方法可用,任何想法放在哪里?我尝试了配方和solo.rb的顶部,但没有运气。

1 个答案:

答案 0 :(得分:1)

https://github.com/sensu/sensu-chef/pull/14#issuecomment-16521845

HOME = "#{ENV['HOME']}"

app_config = data_bag_item("config","app")
override_config = data_bag_item("config","override")

# merging override into app
Chef::Mixin::DeepMerge.deep_merge! override_config["database.yml"], app_config["database.yml"]

template "#{HOME}/app/config/database.yml" do
  local true
  source "#{HOME}/app/config/database.yml.erb"
  variables app_config["database.yml"]
end